3

How do I delete the memory that have been allocated for array models, names? I tried every method but it always crashes when I run it.

int main()
{

    vector<Person*> people;

    const int PERSON_SZ = 4;
    char * names[] = {"Jim", "Fred", "Harry", "Linda"};
    int ages[] = { 23, 35, 52, 59 };

    for (int i = 0; i < PERSON_SZ; i++)
    {

        Person * temp = new Person(names[i], ages[i]);
        people.push_back(temp);
    }

    // A vector of Car pointers
    vector<Car*> cars;

    const int CAR_SZ = 3;
    char * models[] = { "Festiva", "Ferrarri", "Prius" };
    srand(time(0));
    for (int i = 0; i < CAR_SZ; i++)
    {

        Car * temp = new Car(models[i]);
        temp->set_driver(people[rand() % (people.size())]);
        temp->set_owner(people[rand() % (people.size())]);
        cars.push_back(temp);
    }



    for (int p = 0; p < people.size(); p++)
    {
        people[p]->increment_age();
    }

    for (int c = 0; c < cars.size(); c++)
    {
        cars[c]->print();
    }

    delete [] names;
    for ( int r = 0; r < CAR_SZ; ++r )
       {
           delete [] models[r];
       }
    return 0;
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72

7 Answers7

3

You didn't allocate models and names using new, so you cannot delete them.

Both arrays are allocated on the stack and are automatically deleted, when the function returns.

The contents of the (string) arrays (i.e. the strings itselves) are stored in the global data segment and cannot be freed at all. This would also be dangerous because the compiler might use the same string constant at different places in the program.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
MartinStettner
  • 28,719
  • 15
  • 79
  • 106
2

The models array and the names array are both statically allocated. You didn't use new to create them, you don't need to delete them. You should, however, change their types as follows:

const char * names[] = {"Jim", "Fred", "Harry", "Linda"};
const char * models[] = { "Festiva", "Ferrarri", "Prius" };

because they are pointers to string literals, which are read-only.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • so what should i delete? because there are 14 leaked objects when i run it Leaked object at 003E3BA8 (size 8, 00402A43) Leaked object at 003E3C08 (size 18, 00432389) Leaked object at 003E3C48 (size 8, 00402A43) Leaked object at 003E3C78 (size 18, 00432389) Leaked object at 003E3BD8 (size 8, 00402A43) Leaked object at 003E3CB8 (size 20, 00432389) Leaked object at 003E3CF8 (size 12, 00402C3B) Leaked object at 003E3D60 (size 21, 00432389) Leaked object at 003E3DA0 (size 12, h28.cpp:182) Leaked object at 003E3E08 (size 18, 00432389) Leaked object at 003E3E48 (size 12, 00402C3B) *** 14 leaks found – Richard Nguyen Nov 13 '11 at 08:52
  • used this code to delete vector pointer... thanks for helping mefor (int c = 0; c < cars.size(); c++) delete cars[c]; for (int p = 0; p < people.size(); p++) delete people[p]; return 0; – Richard Nguyen Nov 13 '11 at 08:57
2

The variable names and models are not dynamically allocated, and neither is the data in those arrays. So no need to free them or their contents.

The two vector contains data that need to be free'd on the other hand.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

You did not allocate names, and models. You don't need to free or delete them from memory.

Neigyl R. Noval
  • 6,018
  • 4
  • 27
  • 45
2

As others have answered, this is neither required nor legal...

delete [] names;

Since you didn't new names.

Neither is this...

for ( int r = 0; r < CAR_SZ; ++r )
{
     delete [] models[r];
}

Since you didn't new any of the models.

But you do still have some memory leaks!

For each car instance you newed and stored in the cars vector, you need to call delete. Here is one way:

for (std::vector<Car*>::iterator car = cars.begin(), done = cars.end(); car != done; ++car)
{
    delete *car;
}

Note that the * there dereferences the iterator, yielding the raw pointer you newed.

The problem with this approach is that it leaves you with a vector full of dangling pointers. You could either reset them in the loop or clear() the vector after the loop has terminated. Perhaps a better approach would be:

while (not cars.empty())
{
    delete cars.back();
    cars.pop_back();
}

Which deletes each car, then removes its pointer from the vector.

Similarly for each person you newed and stored in people:

while (not people.empty())
{
    delete people.back();
    people.pop_back();
}

As a rule of thumb, there should be one delete for every new and one delete[] for every new ... []. But you'll do well to learn about smart pointers.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
1

names and models are not pointing heap-allocated area. Don't delete it.

Inbae Jeong
  • 4,053
  • 25
  • 38
1

Your code is incomplete. It does not compile. Please enable all warnings and debugging for your compiler, e.g. with g++ -Wall -g if using GCC and improve your code till you get no warning

char * names[] = {"Jim", "Fred", "Harry", "Linda"};
delete [] names;

This is incorrect. You can only delete something that you obtained thru new.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547