?
Solved

Array pointer deletion problem, Heap Corruption

Posted on 2007-04-02
9
Medium Priority
?
1,068 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:b3n_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 3

Expert Comment

by:Darrylsh
ID: 18840908
Can we see some code, especially anything after the delete.  If I had to guess you are using your pointer after you delete it and it might not be obvious such as

delete[] compareBuffer;
*compareBuffer = 0; \\error
0
 

Author Comment

by:b3n_
ID: 18840933
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)fileReader.tellg()-1);                        
                              fileReader.read(compareBuffer, sizeof compareBuffer);
                              for(int i = 0; i < patternSize; i++){
                                    match = (unsigned char)compareBuffer[i] == searchPattern[i];
                                    if(!match){
                                          break;
                                    }
                              }
                              if(!match){
                                    fileReader.seekg((int)fileReader.tellg() - 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;
      }
0
 
LVL 30

Expert Comment

by:Axter
ID: 18841320
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 18841330
>> fileReader.read(compareBuffer, sizeof compareBuffer);

The above line of code is not correct.  the sizeof compareBuffer, will be the size of the pointer, and not the size of the allocated  buffer.
Should be the following:
fileReader.read(compareBuffer, patternSize);

>>matchOffsets = new int[256];
You should avoid using a dynamic array, if you need a small fixed size array.
Since the size of the above array is known at compile time, you can just declare a concrete variable.

int matchOffsets[256] = {0};

This is safer, and more efficient.
0
 

Author Comment

by:b3n_
ID: 18841379
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.
0
 

Author Comment

by:b3n_
ID: 18841517
i found the problem with this code snippet. the problem lies in this line:  fileReader.read(compareBuffer, 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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18841731
>>>> 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
0
 
LVL 30

Expert Comment

by:Axter
ID: 18842367
>>i found the problem with this code snippet. the problem lies in this line:  fileReader.read(compareBuffer, 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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question