我有一个名为 Sample
的类,它包含一个类型为 std::string
的参数 value
。
有一个公共(public)成员函数setValue(std::string tempdata)
,用于设置该成员的值。
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 )
谁能建议我应该如何调试这个问题?
最佳答案
您显示的代码是正确的。所以错误是在别的地方。例如,它可能在较早的某个时间点发生堆栈或堆损坏,直到执行这些代码行后才被注意到。
关于c++ - std::string 赋值时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455926/