我的代碼:因無效鑄造造成的堆損壞?
unsigned char *myArray = new unsigned char[40000];
char pixelInfo[3];
int c = 0;
while(!reader.eof()) //reader is a ifstream open to a BMP file
{
reader.read(pixelInfo, 3);
myArray[c] = (unsigned char)pixelInfo[0];
myArray[c + 1] = (unsigned char)pixelInfo[1];
myArray[c + 2] = (unsigned char)pixelInfo[2];
c += 3;
}
reader.close();
delete[] myArray; //I get HEAP CORRUPTION here
一些測試後,我發現它是由while循環中投造成的,如果我使用一個符號的字符數組myArray我不明白的錯誤,但我必須使用unsigned char代碼的其餘部分。 將pixelInfo轉換爲無符號字符也會產生相同的錯誤。
有沒有解決這個問題的方法?
'!eof()'條件的詛咒返回。 –
而你是**確定**你沒有寫過數組的邊界? – StoryTeller
好吧,你是幾內亞豬。你能告訴我們所有的*爲什麼*你認爲'!reader.eof()'是一個合理的條件?請詳細解釋你的推理,或者你的信息來源。看起來,每個使用C++的人都會犯這個錯誤,我真的很想知道這個普遍的誤解是從哪裏來的。 –