Reputation: 21
I'm doing a practice problem and the question asks to create a destructor to ensure there isn't any memory leak. When I use this destructor, I am getting this error, after executing system("pause");:
https://i.sstatic.net/mWu0A.jpg
Here is the copy constructor:
vector_of_int::vector_of_int ( const vector_of_int& a_vector )
{
an_array = new int[ a_vector.size ];
this->size = a_vector.size;
for( int i = 0; i < size; ++i )
{
an_array[i] = a_vector.an_array[i];
}
}
and the assignment operator:
vector_of_int& vector_of_int::operator= ( const vector_of_int& a_vector )
{
if( this == &a_vector )
{
return *this;
}
this->size = a_vector.size;
for( int i = 0; i < size; ++i )
{
an_array[i] = NULL;
an_array[i] = a_vector.an_array[i];
}
return *this;
}
I searched online a bit and it was mentioned that it could be due to the copy constructor pointing to the same memory location. To test this, in my main() function, I pushed data into each vector, a, b, c, and reprinted them and they were all different. This error shows after the destructor is called and it proceeds to the next line system("pause"); where after pressing any key, it shows. Here is the end of the main():
a_vector.~vector_of_int();
b_vector.~vector_of_int();
c_vector.~vector_of_int();
cout << "\n";
system("pause");
return 0;
Is the main.exe calling the destructors again after the curly braces end of main? When I comment all 3 destructor statements, the error doesn't show anymore.
Thanks.
Upvotes: 1
Views: 1499
Reputation: 371
In the assigment operator you copy items form one array to another. But what if local array has smaller size than parameter passed to operator? You will exceed array's size.
Upvotes: 1
Reputation: 208323
It looks from the code that you are explicitly calling the destructor. While that is legal and has some uses, more often than not you should not explicitly call the destructors, as they will be called automatically when the objects fall out of scope (or delete
is called for pointers). It is undefined behavior to execute the destructor on an already destroyed object.
In your particular code, the destructor is probably releasing memory, the second time (automatic call at the end of the scope of the local variable) it tries to release memory that was already released by your manual call and that triggers a runtime error.
Upvotes: 2
Reputation: 24846
I suppose you've created you vector like this:
vector_of_int v;
Don't delete it then - it's automatic storage. It will be deleted automatically at the end of the scope
In case you've created your vector as:
vector_of_int *v = new vector_of_int();
Call delete
operator to remove it:
delete v;
Upvotes: 1
Reputation: 121961
Don't explicity invoke the destructor, that will happen automatically when the object goes out of scope (from the code, I believe the vectors are stack allocated given the .
notation used to invoke the destructor). Even if they were allocated on the heap, using new
, you would still not invoke the destructor explicitly either but use delete
.
Also, in the assignment operator the this->size
is updated but the an_array
is not. If a_vector.size > this->size
then it will result in out of bounds access on an_array
as it will not have enough elements: delete[]
and new[]
an_array
.
Upvotes: 9