Thread: How do I clean up a dynamic array of pointers to objects?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    How do I clean up a dynamic array of pointers to objects?

    Hey guys, I'm having problems cleaning up a queue that is a dynamic array of pointers to objects. When I call my destructor, the program crashes. Here is my code..

    Private section of class..

    Code:
    private:
    	OBJECT ** array; //declare pointer to pointer of OBJECT
    	int maxSize; //size of array
    Constructor...

    Code:
    queue::queue( unsigned x ){
                  maxSize = x;
                  array = new OBJECT *[x]; //create dynamic array of size x
                  for ( int c = 0; c < x; c++ )
    		          array[c] = NULL; //initialize each element of array to point to NULL
    }
    Destructor that crashes my program...

    Code:
    queue::~queue(){
                    for( int c = 0; c < maxSize; c++ ){//problem here
                         if( array[c] != NULL ) 
                             delete array[c]; //deletes OBJECT if there's one at that element
                    }
                    delete array; // deletes dynamic array
    }
    I'm a little new to dynamic arrays, any help would be appreciated. Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When I call my destructor, the program crashes.
    Can you be more specific? How does it crash? Do you get any error messages? What line does it crash on? Are the contents of your pointers valid?
    Code:
    if( array[c] != NULL ) 
      delete array[c];
    There's no need to test for a null pointer; delete does the right thing.

    >delete array; // deletes dynamic array
    Undefined behavior. When you use new[] to allocate memory, you must use delete[] to release it:
    Code:
    delete [] array;
    >delete array[c];
    This may have the same problem depending on how you allocate memory to array[c].
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Prelude,

    I assign objects to the array by doing

    Code:
    array[variable] = new OBJECT;
    Anyway, I found the problem. It was related to something I hadn't posted. In my problem I would declare the class and pass it to another function that did the manipulation of the class. However, I didn't pass by reference I would send a copy of the class. When I passed by reference the code I typed above works and successfully clears the dynamic array of any classes without any problems. I included your suggestion of using

    Code:
    delete[] array;
    instead of

    Code:
    delete array;

    The thing is that either of those statments seem to work without causing any problems, curious. I have one more question...

    Does delete [] array clear delete any dynamically assigned elements of the array (the OBJECTs) as well as the array itself? If this were true than all I would need is to declare delete [] array in my destructor, I wouldn't have to do that for loop.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The thing is that either of those statments seem to work without causing any problems, curious.
    That's the problem with undefined behavior. It can work perfectly until you demo your application for the client.

    >Does delete [] array clear delete any dynamically assigned elements of the
    >array (the OBJECTs) as well as the array itself?
    No, for every allocation there must be a matching release.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Make sure you have a valid copy constructor and copy assignment operator for your class. Dynamically allocated memory like you are using requires you to write explicit copying code. In this case the rule of three applies- since you needed to write your own destructor, you also need to write your own copy constructor and copy assignment operator. These might have helped avoid the original error (although passing by reference still might be appropriate).

    Also, C++ has features that allow you to avoid these kinds of headaches and mistakes. For example, instead of a dynamic array, use a vector (or some other appropriate container). If you did, you wouldn't have to know how or when to delete [] array, because the vector does that part for you. You can also use some types of smart pointers for the objects you are storing. A smart pointer will delete the object automatically when it goes out of scope.

    In this case, you could use a vector<shared_ptr<OBJECT> > and you wouldn't even need a destructor or copy constructor or copy assignment operator. Another option is a Boost ptr_vector which also does the same things for you but with a more specific interface.

    Of course, since you are writing a queue class, you are apparently doing so as a learning exercise (otherwise you would use an existing class). In that case it would be fine to learn and understand the use of dynamic memory allocation, although learning the other C++ options might be just as enriching.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  3. clean array of blanks, array subscripting
    By kocika73 in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 03:18 PM
  4. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 09:26 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM