memset to struct 引起的 core
#include<string.h>usingnamespacestd;structA{inta;intb;};structB{inta;charaa[10];charab[30];intb;charni[40];stringcs;};intmain(){/*Amya;mya.a=3;mya.b=4;void*pa=(void*)&mya;B*pb=(B*)pa;*/Bba;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#00x00007ff0978960d7in__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#2std::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#30x00007ff097896130instd::basic_string<char,std::char_traits<char>,std::allocator<char>>::~basic_string(this=Unhandleddwarfexpressionopcode0xf3)at/data/home/root/source/build_gcc4.7.4/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/basic_string.h:535#40x0000000000400c00inB::~B(this=0x7fff35938f40,__in_chrg=<valueoptimizedout>)attest_struct.cpp:13#50x0000000000400b63inmain()attest_struct.cpp:39
原因:
因为,memset(&,0,sizeof()) 会把 struct 结构体内的 所有复位 为0,内含的 string 对象 被毁坏了,在析构时 string对象的析构调用问题,
对应 struct 内含 对象 最好不要用 memset 这类函数
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。