I have this code:
ifstream reader;
reader.open("myFile.bmp", ios::binary);
//Read the headers and stuff, fill some variables
unsigned char *bitmapArray = new unsigned char[imageWidth * imageHeight * 4]; //array with enough values for RGBA of the BMP
int c = 0;
while(!reader.eof())
{
reader.read(pixelInfo, 3); //pixelInfo is a char[3]
//Filling RGBA info into my bitmapArray
bitmapArray[c] = pixeInfo[0];
bitmapArray[c + 1] = pixeInfo[1];
bitmapArray[c + 2] = pixeInfo[2];
bitmapArray[c + 3] = 255;
c += 4; //increasing C for the next values
}
reader.close();
//Here I use bitmapArray for some stuff
delete[] bitmapArray; //This gives me HEAP CORRUPTION error
If I don't mess with bitmapArray inside the while loop (simply commenting 4 lines, still leaving the reader there), it goes through fine.
The size of the image I'm using is 40000 bytes long, if I declare the array as "unsigned char bitmapArray[40000];" I still get the same error, but if I add a single char it doesn't - "unsigned char bitmapArray[40001];"
I checked the address the debugger says it's corrupted, but it isn't nowhere close to the starting/ending addresses of bitmapArray.
The error is: "HEAP CORRUPTION DETECTED: after Normal block (#161) at 0x00413F58. CRT detected that the application wrote to memory after end of heap buffer."
Why is this happening?