我有一個名爲Sample
的類,其中包含參數value
,它的類型爲std::string
。 有一個公共成員函數setValue(std::string tempdata)
用來設置這個成員的值。std :: string賦值時的分段錯誤
void Sample::setValue(std::string tempdata) { this->value= tempdata; }
從應用程序我必須設置此參數的值。 我做這樣的事情:
std::string tempvalue = "Hello";
Sample s;
s.setValue(tempvalue);
當我運行該應用程序崩潰,並通過gdb調試它,我得到:
#0 0x049da761 in __gnu_cxx::__exchange_and_add() from /usr/lib/libstdc++.so.6
#1 0x049c0e6e in std::string::assign() from /usr/lib/libstdc++.so.6
#2 0x049c0ed1 in std::string::operator=() from /usr/lib/libstdc++.so.6
#3 0x08075e9b in Sample::setValue (this=0x83779a8, tempdata=Cannot access memory at address 0xffffffff)
任何人都可以請建議我應該如何去調試這個問題?
代碼看起來沒問題。您是否修改了頭文件而不重新編譯包含它的文件? (例如,如果您正在使用Makefile並定義這些目標,請執行make clean然後執行make) –
這看起來像是gcc的內部問題,請參閱http://stackoverflow.com/questions/ 7038124/weird-sigsegv-segmentation-fault-in-stdstringassign-method-from-libstdc。 –