开发者

Segmentation Fault while std::string assignment

开发者 https://www.devze.com 2023-04-05 08:19 出处:网络
I have a class named Sample which contains a parameter value which is of type std::string. There is a public member function setValue(std::string tempdata) which is used to set the value of this membe

I have a class named Sample which contains a parameter value which is of type std::string. There is a public member function setValue(std::string tempdata) which is used to set the value of this member.

void Sample::setValue( std::string tempdata ) { this->value= tempdata; }

From the application I have to set the value of this parameter. I do something like:

std::string tempvalue 开发者_运维百科= "Hello";
Sample s;
s.setValue( tempvalue );

when I run the application the program crashes and on debugging it through gdb I get:

#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 )

Can anyone please suggest how should I go about debugging this issue?


The code you've shown is correct. So the error is somehere else. It could, for example be stack or heap corruption at some earlier point, which just isn't noticed until these lines of code are executed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号