Dynamic Issue
Oct 22, 2012 at 2:12am UTC
Hi, I am getting an "aborted(core dumped)" when I include the destructor. Could someone help understand what is wrong?
1 2 3 4 5 6 7
const int Size = 100;
class test
{
private :
int * array;
};
1 2 3 4 5 6 7
//Constr
test::test()
{
array = new int [Size];
for (int i = 0; i < Size; i++)
array[i] = -1;
}
1 2 3 4 5 6 7
//destructor
test::~test()
{
for (int i = 0; i < Size; i++)
delete [] array;
}
Oct 22, 2012 at 2:16am UTC
You don't need to use for loop inside destructor. Your array is one dimensional. Just
delete array;
is enough
Oct 22, 2012 at 2:20am UTC
You're trying to deallocate memory "Size" times. After the first call to delete the memory location will become invalid and generate an error if you try to use it again
Oct 22, 2012 at 2:46am UTC
Okay, I see I ran the code in the codeblocks compiler and it worked with no errors. Anyway Thanks guys
Topic archived. No new replies allowed.