Reputation: 369
Here I am declaring a pointer's array and then calling print()
method from class A
A *ptr1[10];
ptr1[0]= new A;
ptr1[0]->print();
Above works fine but when I try to delete it shows assertion failed error
delete[] ptr1;
I am using Visual studio 2010
Details of error:
Upvotes: 0
Views: 362
Reputation: 3689
The Good way to do:
#define SIZE 10
A *ptr1[SIZE];
// allocate and do print
for(int i = 0; i < SIZE; i++)
{
ptr1[i]= new A();
ptr1[i]->print();
}
// deletion
for(int j = 0; j < SIZE; j++)
{
if(ptr1[j] != NULL)
{
delete ptr1[j];
ptr1[j] = NULL;
}
}
Upvotes: 1
Reputation: 8171
ptr1
is an an array of pointers to A. Since you didn't allocated ptr1 itself via new
, then you shouldn't delete
it.
ptr1[0]
is a pointer to an A that you allocated. So you would just need to do delete ptr1[0]
.
Upvotes: 3