4

If I have a class with an array of pointers to another class Vehicle :

class List {
    public:
        //stuff goes here
    private:
        Vehicle ** vehicles;
}

If I now write the destructor of the class List, do I manually iterate over the array (I know how many items are in the array) and delete every pointer to a vehicle, or will C++ automatically call the destructors of all the Vehicles in the array?

(Like it does if there's a private string/... in the class or if it would be a STL container of Vehicle pointers)

EDIT: I forgot about delete [] vehicles, but if I would do that, would it also delete the memory used by all the vehicles in the array, or would it just delete the memory used by the pointers?

3
  • Use vector and boost::scoped_ptr. If you use C++0x, use vector and std::unique_ptr. Commented Jan 20, 2011 at 15:06
  • @Alexandre this is a small question of a previous exam of a course i'm taking, i would use a vector if it would be allowed on that exam :) Commented Jan 20, 2011 at 15:08
  • @Alexandre: haha, fortunately this is the only time they use this and we use vectors etc. any other time. I believe his intentions were to make something clear about polymorphism and arrays or something... Commented Jan 20, 2011 at 15:14

4 Answers 4

7

You have to delete all the entries in the array AND delete the array. There are methods in C++ (STL) to avoid this: use a vector, so you don't have to delete the array. Use scoped_ptr/shared_ptr per Vehicle, so you don't have to delete the vehicles.

Sign up to request clarification or add additional context in comments.

1 Comment

One clarification, STL is only a subset of C++ Standard Library. shared_ptr is not part of STL, but C++ Standard Library (as defined in upcoming C++0x as well as included in Boost collection). scoped_ptr is not part of any version of C++ Standard Library, only Boost.
3

If the List owns Vehicle objects (creates them in the constructor) you need to delete every single one and then delete the array of pointers itself.

1 Comment

+1: Regardless of the available better options like STL it's important when learning a language to grasp this concept. A rule to make special note of is if you use new[] to allocate you must use delete[] to deallocate or you will run into corruption issues.
2

If i have a class with an array of pointers to another class Vehicle :

Vehicle ** vehicles;

vehicles is not an array of pointers rather its a pointer to pointer to a Vehicle type. An array of pointers would be defined something like Vehicle* vehicles[N].

do i manually iterate over the array (i know how many items are in the array) and delete every pointer to a vehicle

Yes! You dont want your code to leak memory do you?

I would recommend using Boost::scoped_ptr from the Boost library. Moreover if you compiler supports C++0x you can also use std::unique_ptr

1 Comment

This is how my professor explained it in an exercise, and honestly, i also thought it was Vehicle * vehicles[n] :)
0

You have to manually iterate over vehicles and delete each and every of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.