您好,登錄后才能下訂單哦!
#include<string.h> using namespace std; struct A { int a; int b; }; struct B { int a; char aa[10]; char ab[30]; int b; char ni[40]; string cs; }; int main() { /*A mya; mya.a=3; mya.b=4; void * pa =(void*)&mya; B* pb=(B*)pa;*/ B ba; memset(&ba,0,sizeof(B)); /*cout<<sizeof(mya)<<endl; cout<<sizeof(B)<<endl; cout<<pb->a<<endl; cout<<pb->b<<endl; cout<<pb->aa<<endl; cout<< pb->ni<<endl;*/ //cout<< pb->cs; }
產生的 core 文件
gdb a core.***
bt
(gdb) bt #0 0x00007ff0978960d7 in __exchange_and_add (this=0xffffffffffffffe8, __a=...) at /data/home/root/source/build_gcc4.7.4/x86_64-unknown-linux-gnu/libstdc++-v3/include/ext/atomicity.h:48 #1 __exchange_and_add_dispatch (this=0xffffffffffffffe8, __a=...) at /data/home/root/source/build_gcc4.7.4/x86_64-unknown-linux-gnu/libstdc++-v3/include/ext/atomicity.h:81 #2 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_M_dispose (this=0xffffffffffffffe8, __a=...) at /data/home/root/source/build_gcc4.7.4/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/basic_string.h:242 #3 0x00007ff097896130 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string (this=Unhandled dwarf expression opcode 0xf3 ) at /data/home/root/source/build_gcc4.7.4/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/basic_string.h:535 #4 0x0000000000400c00 in B::~B (this=0x7fff35938f40, __in_chrg=<value optimized out>) at test_struct.cpp:13 #5 0x0000000000400b63 in main () at test_struct.cpp:39
原因:
因為,memset(&,0,sizeof()) 會把 struct 結構體內的 所有復位 為0,內含的 string 對象 被毀壞了,在析構時 string對象的析構調用問題,
對應 struct 內含 對象 最好不要用 memset 這類函數
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。