b3n_
asked on
Array pointer deletion problem, Heap Corruption
Hello,
i have a problem with deleting an array pointer:
char* compareBuffer = new char[patternSize];
This is how i create the array, patternSize is an int.
When i try to do delete[] compareBuffer; later on in the program, i get the following error reported:
-------------------------- ---------- -------
Debug Error!
.....
HEAP CORRUPTION DETECTED: after Normal block (#196) at 0x003694C8. CRT detected that the application wrote to memory after end of heap buffer.
-------------------------- ---------- --
Can someone explain what this means and how i can fix it? (If it is important, I'm using Visual Studio 2005)
Thank you.
i have a problem with deleting an array pointer:
char* compareBuffer = new char[patternSize];
This is how i create the array, patternSize is an int.
When i try to do delete[] compareBuffer; later on in the program, i get the following error reported:
--------------------------
Debug Error!
.....
HEAP CORRUPTION DETECTED: after Normal block (#196) at 0x003694C8. CRT detected that the application wrote to memory after end of heap buffer.
--------------------------
Can someone explain what this means and how i can fix it? (If it is important, I'm using Visual Studio 2005)
Thank you.
ASKER
This is the code:
bool findPattern()
{
delete[] matchOffsets;
matchOffsets = new int[256];
char readChar;
std::fstream fileReader;
fileReader.open(fileName, std::ios::in | std::ios::binary);
if(fileReader.is_open()){
while(!fileReader.eof()){
fileReader.read(&readChar, sizeof readChar);
if((unsigned char)readChar == searchPattern[0]){
bool match = true;
char* compareBuffer = new char[patternSize];
fileReader.seekg((int)file Reader.tel lg()-1);
fileReader.read(compareBuf fer, sizeof compareBuffer);
for(int i = 0; i < patternSize; i++){
match = (unsigned char)compareBuffer[i] == searchPattern[i];
if(!match){
break;
}
}
if(!match){
fileReader.seekg((int)file Reader.tel lg() - patternSize + 1);
} else {
int fileOffset = (int)fileReader.tellg() - patternSize;
matchOffsets[matchCounter+ +] = fileOffset;
}
//Here comes the delete
delete[] compareBuffer;
}
}
} else {
std::cout << "Error opening file";
return false;
}
fileReader.close();
return true;
}
bool findPattern()
{
delete[] matchOffsets;
matchOffsets = new int[256];
char readChar;
std::fstream fileReader;
fileReader.open(fileName, std::ios::in | std::ios::binary);
if(fileReader.is_open()){
while(!fileReader.eof()){
fileReader.read(&readChar,
if((unsigned char)readChar == searchPattern[0]){
bool match = true;
char* compareBuffer = new char[patternSize];
fileReader.seekg((int)file
fileReader.read(compareBuf
for(int i = 0; i < patternSize; i++){
match = (unsigned char)compareBuffer[i] == searchPattern[i];
if(!match){
break;
}
}
if(!match){
fileReader.seekg((int)file
} else {
int fileOffset = (int)fileReader.tellg() - patternSize;
matchOffsets[matchCounter+
}
//Here comes the delete
delete[] compareBuffer;
}
}
} else {
std::cout << "Error opening file";
return false;
}
fileReader.close();
return true;
}
Is matchOffsets being initialized to NULL?
If it's not, this can give you a runtime error, since it's content's can be pointing to anything.
I recommend you use std::vector<char> instead of the above code.
std::vector is a safer way to create a dynamic array, and you don't have to worry about clean up via delete, because the array cleans up after itself.
If it's not, this can give you a runtime error, since it's content's can be pointing to anything.
I recommend you use std::vector<char> instead of the above code.
std::vector is a safer way to create a dynamic array, and you don't have to worry about clean up via delete, because the array cleans up after itself.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
its not about the matchOffsets its about the compare buffer. The rest of the code works fine, i just get problems when i try to delete the compareBuffer after i used it. And in the case of the compareBuffer i dont know how big it will be so i need to use dynamic arrays.
ASKER
i found the problem with this code snippet. the problem lies in this line: fileReader.read(compareBuf fer, sizeof compareBuffer); i shouldnt use sizeof comparebuffer, because it returns 4 while the actual buffer size is just 3.
thanks all for commenting on this question.
thanks all for commenting on this question.
>>>> HEAP CORRUPTION DETECTED:
That is cause you have written beyond the allocated boundaries of the char array. If you allocate a char array in debug mode it is allocated some more space for the debugger. The debugger writes a signature at end which was checked at deletion. If you have written beyond boundaries the signature most likely was destroyed, hence the debug assertion.
Regards, Alex
That is cause you have written beyond the allocated boundaries of the char array. If you allocate a char array in debug mode it is allocated some more space for the debugger. The debugger writes a signature at end which was checked at deletion. If you have written beyond boundaries the signature most likely was destroyed, hence the debug assertion.
Regards, Alex
>>i found the problem with this code snippet. the problem lies in this line: fileReader.read(compareBuf fer, sizeof compareBuffer); i shouldnt use sizeof
>>comparebuffer, because it returns 4 while the actual buffer size is just 3.
That's what I posted in my second comment.
>>comparebuffer, because it returns 4 while the actual buffer size is just 3.
That's what I posted in my second comment.
delete[] compareBuffer;
*compareBuffer = 0; \\error