heap corruption after vector push_back
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: heap corruption after vector push_back

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Exclamation heap corruption after vector push_back

    I have been stuck in one problem of heap corruption.
    The problem i traced occurs after vector push_back.

    Code:
    //I have two structures in cpp
    struct name
    {
    int a;
    int b;
    char address[5];
    name *ptr;
    };
    
    typedef vector<name>  vecName;
    typedef vector<name*> vecNamePtr;
    
    //Second one
    struct part
    {
    vecName vcN;
    VecNamePtr vcNp;
    int a;
    char address[5];
    int c;
    }
    
    typedef vector<part>  vecPart;
    typedef vector<part*> vecPartPtr;
    
    //Somewhere down the line, I have created pointer of struct name.
    name **FilledData;
    *FilledData=  (name*) calloc(*rows, sizeof(name)); // rows calculated somewhere to keep data array.
    
    //after this data is poulated in array.
    for(i=0; i<rows; i++){
    *FilledData[i] // Fill the structure with data;
    (*FilledData)[i].ptr=(*FilledData)[i];
    }
    
    {
    //After this trying to insert the data in vector
    vecPart pList;
    part pPart;
    
    pPart = uciList[i]; // For this i have parametrized constructor like part (const name&)
    //The data in filledDatat[i].ptr is Ok till now.
    pList.push_back(pPart); //I have the copy constructor part (const part&)
    //The data in filledData[i].ptr is CORRUPTED.
    }
    Please help, and let me know if more information required.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: heap corruption after vector push_back

    Quote Originally Posted by @bhi View Post
    I have been stuck in one problem of heap corruption.
    The problem i traced occurs after vector push_back.
    First, why are you writing a program in this manner when you're using C++? It looks more like a 'C' program than a C++ program. You don't need all of this pointer manipulation, dynamically allocated memory, etc. to accomplish whatever you're trying to accomplish.

    Having said this, the problem is that the struct "name" you are placing in the vector is not safely copyable or assignable. It lacks a proper copy constructor and assignment operator to handle the pointer variable. It also lacks a proper destructor.

    When you place an item in a vector, that item has to be safely copyable and destroyable.
    pList.push_back(pPart); //I have the copy constructor part (const part&)
    Why not post a full program that demonstrates the error. Don't describe what you are doing, post the complete, but smallest example that duplicates the issue. How can we know that what you're doing is correct or not if you don't post the code you're describing?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 23rd, 2013 at 02:15 AM.

  3. #3
    Join Date
    Oct 2012
    Posts
    2

    Re: heap corruption after vector push_back

    I found the reason, Actually *ptr memory in structure was destroyed already by the bad usage of realloc() and when push_back in vector it tried to refer but didn't find data.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,994

    Re: heap corruption after vector push_back

    Having said this, the problem is that the struct "name" you are placing in the vector is not safely copyable or assignable. It lacks a proper copy constructor and assignment operator to handle the pointer variable. It also lacks a proper destructor.
    Until your 'name' and 'part' structures have correct copy constructors and assignment operators then your program will still have problems.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,639

    Re: heap corruption after vector push_back

    and don't use malloc, calloc, free in a C++ program (even if the standard library in your code ends up using those).

    If you're doing C++, use new/delete.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: heap corruption after vector push_back

    Quote Originally Posted by @bhi View Post
    I found the reason, Actually *ptr memory in structure was destroyed already by the bad usage of realloc()
    Which you didn't show.
    and when push_back in vector it tried to refer but didn't find data.
    I am going to place a bet that this is not the only problem in your code.

    Why do you need both a vector<name> and vector<name*>? Whenever I see that, then that is a red flag that something is wrong -- the design is flawed, you're using the wrong container, or you don't fully understand how to avoid doing this.

    Second, are you sure that only 5 characters are needed for "address"?

    If you really do want to create "name", and have a pointer to another name within that name struct, you could have just used a std::list<name> and forget about that pointer to name:
    Code:
    #include <list>
    #include <cstring>
    
    struct name
    {
        int a;
        int b;
        char address[5];
    };
    
    std::list<name> NameList;
    
    int main()
    {
       name n1;
       n1.a = 10;
       n1.b = 20;
       strcpy(n1.address, "abc");
       NameList.push_back(n1);
       name n2;
       n2.a = 10;
       n2.b = 20;
       strcpy(n2.address, "def");
       NameList.push_back(n2);
    }
    I've just created a list of names, and n1 is "linked" to n2.

    Third, what is the purpose of this code?
    Code:
    name **FilledData;
    *FilledData=  (name*) calloc(*rows, sizeof(name)); // rows calculated somewhere to keep data array.
    If it is to create a dynamic array (whether 1-dimensional or 2-dimensional) of type name, why? You have std::vector<>, so why don't you use it for what it's inteded for? A vector is a dynamic array.
    Code:
    std::vector<name> FilledData(*rows);  // creates an array of names with *rows entries
    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center