Search code examples
c++debuggingvisual-studio-debugging

Getting assertion failed error while deleting dynamic memory


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:

enter image description here


Solution

  • 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].