Roi Avram Roi Avram - 1 year ago 88
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:




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 Source

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[]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download