快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

memsettostruct引起的core

#include
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<a<b<aa<ni<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, std::allocator >::_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, std::allocator >::~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=) at test_struct.cpp:13
#5  0x0000000000400b63 in main () at test_struct.cpp:39

原因:

因为,memset(&,0,sizeof()) 会把 struct 结构体内的 所有复位 为0,内含的 string 对象 被毁坏了,在析构时 string对象的析构调用问题,

对应 struct 内含 对象 最好不要用 memset 这类函数


文章题目:memsettostruct引起的core
文章源于:http://6mz.cn/article/gpsegs.html

其他资讯