memset to struct 引起的core
it = {first = <error reading variable: Cannot access memory at address 0xffffffffffffffe8>, second = {
m_Deviceid = <error reading variable: Cannot access memory at address 0xffffffffffffffe8>,
m_Loopbackip = <error reading variable: Cannot access memory at address 0xffffffffffffffe8>, m_Creator = "10.27.88.149",
m_Createtime = "", m_editor = <error reading variable: Cannot access memory at address 0xffffffffffffffe8>,
m_edittime = 0, m_flag = 0}}
?string对象在创建的时候会使用构造函数初始化类成员,memset把初始化的结果全部都清除了,string就废了,所以出错。
因为,memset(&structname,0,sizeof(structname)) 会把 struct 结构体内的所有复位为0,内含的 string对象被毁坏了。所以对于struct内含需要在堆上申请内存的对象,最好不要用 memset这类函数对结构体进去初始化,如果真的需要使用,一定要注意测试周全,防止访问错误内存。
?
|