///EDIT: I did a little more looking and found a function _CrtCheckMemory(), I through it in and found that I was getting a corruption after the first run through of the code listed below. Still not 100% sure why though.
I tracked it down a little further to a for loop that adds information to an array. This sounds like what I've been reading on, however the for statement stays within bounds. And it only has a heap corruption after it exits the loop, I stuck a _CrtCheckMemory() at the end of the inside of the loop and right after it. The only thing that changes after that is the incrementing var increase by one.
for (i=0;i<size;i++) ///> Some crazy heap problem in here
{ ///> Some crazy heap problem in here
cc = *(c+size-i-1); ///> Some crazy heap problem in here
k = atoi(&cc); ///> Some crazy heap problem in here
this->mStorage[i] = k; ///> Some crazy heap problem in here
_CrtCheckMemory(); ///> Some crazy heap problem in here
} ///> Some crazy heap problem in here
_CrtCheckMemory(); //---> This one notifies me of the heap error.
- for (i=0;i<size;i++) ///> Some crazy heap problem in here
- { ///> Some crazy heap problem in here
- cc = *(c+size-i-1); ///> Some crazy heap problem in here
- k = atoi(&cc); ///> Some crazy heap problem in here
- this->mStorage[i] = k; ///> Some crazy heap problem in here
- _CrtCheckMemory(); ///> Some crazy heap problem in here
- } ///> Some crazy heap problem in here
- _CrtCheckMemory(); //---> This one notifies me of the heap error.
Okay, so I'm running into a problem. I have a function that will "resize" an array for my BigInt class. The function works perfectly fine from what I can see. There just seems to be some bug that gets introduced someplace. After I try to delete some memory I get a Heap Corruption error. Now I've done some reading and found that almost %90 of the time the corruption happens before where ever the compile shows the error to be. I've tracked down the error to occur after try to delete a block of memory.
This is the error I get:
HEAP CORRUPTION DETECTED: after Normal block (#126) at 0x00B44D40
CRT detected that the application wrote to memory after end of heap buffer.
This is the function:
void BigInt::resize ( unsigned long int newSize )
{
unsigned long int i = 0;
unsigned long int goTo = this->mSize;
short int * oldStorage = this->mStorage;
if ( newSize < this->mSize )
{
goTo = newSize;
}
short int * tmpStorage = new short int [newSize];
for (i=0;i<newSize;i++)
{
if( i < this->mSize )
{
tmpStorage[i] = this->mStorage[i];
}
else
{
tmpStorage[i] = 0;
}
}
this->mStorage = tmpStorage;
delete [] oldStorage; // errors after this line executes
this->mSize = newSize;
}
- void BigInt::resize ( unsigned long int newSize )
- {
- unsigned long int i = 0;
- unsigned long int goTo = this->mSize;
-
- short int * oldStorage = this->mStorage;
-
- if ( newSize < this->mSize )
- {
- goTo = newSize;
- }
-
- short int * tmpStorage = new short int [newSize];
-
- for (i=0;i<newSize;i++)
- {
- if( i < this->mSize )
- {
- tmpStorage[i] = this->mStorage[i];
- }
- else
- {
- tmpStorage[i] = 0;
- }
- }
-
- this->mStorage = tmpStorage;
- delete [] oldStorage; // errors after this line executes
-
- this->mSize = newSize;
- }
The error only occurs after I pass through a few different functions, each of which are passing an object onto another. Would this error come up if I were to delete memory that might be being used some place else? But if that is the case wouldn't it only error when I try to access that section of memory again and not when I'm deleting a section?
#define NULL (::rand() % 2)