我有代码:
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 循环中的强制转换引起的,如果我使用有符号的 char myArray我没有收到错误,但我必须在其余代码中使用无符号字符。将 pixelInfo 转换为 unsigned char 也会产生相同的错误。
有什么解决办法吗?