-
June 23rd, 2013, 01:56 AM
#1
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.
-
June 23rd, 2013, 02:12 AM
#2
Re: heap corruption after vector push_back
Originally Posted by @bhi
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.
-
June 25th, 2013, 06:04 AM
#3
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.
-
June 25th, 2013, 06:33 AM
#4
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 25th, 2013, 07:03 AM
#5
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.
-
June 25th, 2013, 05:27 PM
#6
Re: heap corruption after vector push_back
Originally Posted by @bhi
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|