在使用vector的push_back函數時,Release版下運行會出錯的一個原因

   在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;
}


 

發佈了162 篇原創文章 · 獲贊 19 · 訪問量 86萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章