I have c++ code that attempts to dynamically allocate a 2d array of bytes that measures approx 151MB in size. When I attempt to go back and index through the array, my program crashes in exactly the same place every time with an ‘Access violation reading location 0x0110f000’ error, but the indicies appear to be in range. That leads me to believe the memory at those indicies wasn’t allocated correctly.
1) What’s the max number of bytes you can dynamically allocate using the new operator?
2) If it is the case that I’m failing to dynamically allocate memory, would it make sense that my code is crashing when attempting to access the array at exactly the same two indicies every time? For some reason, I feel like they would be different every time the program is run, but what do i know 😉
3) If you don’t think the problem is from an unsuccessful call to new, any other ideas what could be causing this error and crash?
Thanks in advance for all your help!
*Edit
Here’s my code to allocate the 2d array…
#define HD_WIDTH 960 #define HD_HEIGHT 540 #define HD_FRAMES 100 //pHDVideo is a char** pHDVideo->VideoData = new char* [HD_FRAMES]; for(int iFrame = 0; iFrame < HD_FRAMES; iFrame++) { //Create the new HD frame pHDVideo->VideoData[iFrame] = new char[HD_WIDTH * HD_HEIGHT * 3]; memset(pHDVideo->VideoData[iFrame], 0, HD_WIDTH * HD_HEIGHT * 3); }
and here’s a screenshot of the crashing code and debugger (Dead Link) it will help.
I should add that the call to memset never fails, which to me means the allocations is successful, but I could be wrong.
EDIT I found a fix everyone, thanks for all your help. Somehow, and I still need to figure out how, there was one extra horizontal line being upscaled, so I changed…
for(int iHeight = 0; iHeight < HD_HEIGHT; iHeight++)
to
for(int iHeight = 0; iHeight < HD_HEIGHT-1; iHeight++)
and it suddenly worked. Anyhow, thanks so much again!
Some possibilities to look at or things to try:
In answer to your specific questions:
1/ It’s implementation- or platform-specific, and it doesn’t matter in this case. If your new’s were failing you’d get an exception or null return, not a dodgy pointer.
2/ It’s not the case: see (1).
3/ See above for some possibilities and things to try.
Following addition of your screenshot:
You do realize that the error message says ‘Access violation reading …’?
That means it’s not complaining about writing to
pHDVideo->VideoData[iFrame][3*iPixel+2]but reading fromthis->VideoData[iFrame][3*iPixelIndex+2].iPixelIndex is set to 25458, so can you confirm that
this->VideoData[iFrame][76376]exists? I can’t see from your screenshot how this->VideoData is allocated and populated.