Roi Avram Roi Avram - 3 days ago 5
C++ Question

c++ delete[] is causing a break-point and not deleting

I'm trying to free memory but delete[] isnt working for me, I don't think I'm doing anything wrong but maybe you can help me:

Midget.cpp:

Midget::Midget()
{
}

Midget::~Midget()
{
}


Creating and deleting done in main:

Midget **village;

village = new Midget *[num_of_midgets];
for (int i = 0; i < num_of_midgets; i++)
village[i] = new Midget;

for (int i = 0; i < num_of_midgets; i++)
delete[] village[i];
delete[] village;


By the way Midget has no pointer variables in his definition.

When the program runs it stops doing anything when it gets to the delete part and approximately 30 secs later pops out this message:
Error message

Thanks in advance for any help!

Answer

The delete in the for loop should not be an array delete

for (int i = 0; i < num_of_midgets; i++)
    delete village[i];
delete[] village;

You should always match new & delete and new[] & delete[]

Comments