Richard Nguyen
Richard Nguyen

Reputation: 95

Delete array pointer?

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;
}

Upvotes: 3

Views: 998

Answers (7)

johnsyweb
johnsyweb

Reputation: 141770

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.

Upvotes: 2

MartinStettner
MartinStettner

Reputation: 29164

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.

Upvotes: 3

Neigyl R. Noval
Neigyl R. Noval

Reputation: 6038

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

Upvotes: 2

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.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

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.

Upvotes: 2

Inbae Jeong
Inbae Jeong

Reputation: 4103

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

Upvotes: 1

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

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.

Upvotes: 2

Related Questions