在vs2008下,当在一个结构体中有vector类型的成员时,如果在定义了一个该结构体的变量,并使用memset函数对其初始化,在debug版本下并不会有问题。但换成release版本后,程序运行会产生异常,并报如下信息:
Microsoft Visual Studio C Runtime Library has detected a fatal error in STLtest.exe.
Press Break to debug the program or Continue to terminate the program.
该问题主要是由于对结构体变量使用了memset函数,如果结构体中有vector这样的类型,使用memset会导致结构体中的某些信息丢失,从而在使用push_back函数插入数据时产生异常中断。希望我的遭遇对大家有帮助。
- #include "stdafx.h"
- #include <vector>
- #include <iostream>
- using namespace std;
- typedef struct _structBB
- {
- int i;
- vector<int> vBBs;
- }BB;
- int _tmain(int argc, _TCHAR* argv[])
- {
- BB bb;
- memset(&bb,0,sizeof(BB));
- bb.vBBs.push_back(1);
- return 0;
- }