I
have the a problem of the double freeing of memory in the following
program.
The debugger shows that the
issue is in the push_back()
function.
Class
A:
class A
{
public:
A(int x);
int
x;
};
A::A(int x) {
this->x =
x;
}
Class
B:
class B
{
public:
B(int x);
~B();
A*
a;
};
B::B(int x) {
this->a = new
A(x);
}
B::~B() {
delete
a;
}
Main
function:
int
main() {
vector vec;
for(int i = 0; i < 10;
i++) {
vec.push_back(B(i)); <------------ Issue is
here
}
cout << "adding complete"
<< endl;
for(int i = 0; i < 10; i++) {
cout
<< "x = " << (vec[i].a)->x << endl;
}
return
0;
}
What
is wrong in this
code?
EDIT: Error
double free or memory corruption
Answer
Everyone else has
already harped on this so I won't dive further.
To address the usage you are
apparently trying to accomplish (and conform to the Rule of Three in the process of
elimination), try the following. While everyone is absolutely correct about proper
management of ownership of dynamic members, your specific sample can easily be made to
avoid their use
entirely.
Class
A
class A
{
public:
A(int x);
int
x;
};
A::A(int x)
:
x(x)
{
}
Class
B
class B
{
public:
B(int x);
A
a;
};
B::B(int x)
: a(x)
{
}
Main
Program
int main()
{
vector vec;
for(int i = 0; i < 10; i++)
{
vec.push_back(B(i));
}
cout <<
"adding complete" << endl;
for(int i = 0; i <
10; i++) {
cout << "x = " << vec[i].a.x << endl;
}
return
0;
}
Bottom
Line
Don't use dynamic allocation unless you have a
good reason to, and it is lifetime-guarded by contained
variables such as smart pointers or classes that vigorously practice the Rule of
Three.
No comments:
Post a Comment