memset to struct 引起的 core

发布时间:2020-04-13 22:32:28 作者:qdqade
来源:网络 阅读:1256
#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 这类函数

推荐阅读:
  1. Mysql数据库理论基础之五--SELECT单多表查询、子查询、别名
  2. 关于oracle实例恢复的前滚和回滚的理解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

struct memset ct

上一篇:解决dialog在横竖屏切换时消失

下一篇:剑指offer:字符流中第一个不重复的字符

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》