可能是堆被损坏,这也说明 XX.exe 中或它所加载的任何 DLL 中有 bug
今天遇到一個很奇怪的問題,
當代碼如下時:
char* s = (char*)malloc(20*sizeof(char)); string buffer; const char* conchar; char* p; double x = 0.01; while(getline(file,buffer)){const int len = buffer.length();conchar = buffer.c_str();//m_conchar為const char *strcpy(s,conchar);//換成char*cout<<x<<endl; }上述不是我完整的程序,cout是實例,就是每次運行到cout只要輸出是小數(shù)都會出錯,而int類型啥的都不出錯。
當工程設置運行庫“多線程DLL(MD)”
XXX.exe 中的 0x7707e4e4 (ntdll.dll) 處有未經處理的異常: 0xC0000005: 寫入位置 0x0000000000000024 時發(fā)生訪問沖突
當工程設置運行庫“多線程調試DLL(MDd)”
只要是cout或者file.close()或者free(s)都會出現(xiàn)
其原因可能是堆被損壞,這也說明 XX.exe 中或它所加載的任何 DLL 中有 bug
http://blog.csdn.net/blz_wowar/article/details/2176536
但是感覺這些都不適用,沒有自己加載的dll。
之后一點一點注釋,發(fā)現(xiàn)沒有strcpy這個函數(shù)就不會崩潰,而加上之后在cout處還是會崩潰,每次出錯都在C的運行時庫,后來把代碼改成了
//char* s= (char*)malloc(20*sizeof(char)); string buffer; //const char* conchar; char* p; double x = 0.01; while(getline(file,buffer)){const int len = buffer.length();char* s = new char[len];strcpy(s,buffer.c_str());//conchar = buffer.c_str();//m_conchar為const char *//strcpy(s,conchar);//換成char*cout<<x<<endl;//delete s; }這樣可以順利運行,但是delete的時候程序會有問題,這樣就造成了內存泄露,很奇怪。
?
最后搞清楚了這種情況是越界了,所以free會出錯,delete也會出錯,http://blog.csdn.net/kuaile123/article/details/10215483
總結
以上是生活随笔為你收集整理的可能是堆被损坏,这也说明 XX.exe 中或它所加载的任何 DLL 中有 bug的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vs2010连接mongodb服务器,X
- 下一篇: 快速排序 C++