2

I am trying to create a array of pointers dynamically. From what I have read until now this can be done in the following way in C++

CPoint** data_temp;
data_temp = new CPoint*[an_integer]; 

I am later going to add pointers to this array which will be pointing to an object. Some what like this, using a for loop

 CPoint A;
 CPoint* data = &A; //I am using just a single value but data will also be an array
*data_temp[i] = data; // where data is pointer address

Now the question is if to free up memory I delete the pointer array data_temp would it also delete the original object i.e. A?

delete[] data_temp;

I am asking this because I need the object at a later stage.

akash_c
  • 173
  • 1
  • 2
  • 9
  • Related : http://stackoverflow.com/questions/441831/c-calling-delete-on-variable-allocated-on-the-stack – olevegard Aug 20 '13 at 12:17
  • 1
    possible duplicate of [delete a pointer to pointer (as array of arrays)](http://stackoverflow.com/questions/4193982/delete-a-pointer-to-pointer-as-array-of-arrays) – dyp Aug 20 '13 at 12:21
  • Am I the only one that read what the OP is *putting* in that array? If the pointers you're adding are as you show here, no you don't need to free the elements (because you never *allocated* them; they're automatic). You do need to delete the dynamically allocated pointer array (and you are). Were the elements *also* dynamic-allocated memory (i.e. `new`ed) you would need to free them prior to freeing the pointer array. I *strongly* discourage all of this regardless, and suggest you investigate the joys of [`std::vector<>`](http://en.cppreference.com/w/cpp/container/vector) – WhozCraig Aug 20 '13 at 12:24
  • @WhozCraig I have seen on many occasions people advising against the use of `std::vector<>` since it slows down the program and also it allocates a much larger portion of memory than is actually needed. – akash_c Aug 20 '13 at 12:35
  • 2
    @akash_c Whomever those "people" are, they're wrong. There is no silver-bullet, but if you think you can manage memory better than the implementation writers and their *highly* optimized and debugged algorithms in the standard library, you certainly wouldn't be posting questions like this here. That said, one could easily use the standard library *inefficiently*. But the onus is *almost always* on the user; not the lib. It is a tool, and properly used you would be hard pressed to beat it. – WhozCraig Aug 20 '13 at 12:43

4 Answers4

2

delete[] would be the thing to do if the array contained the objects themselves: those objects would be automatically destroyed. However, since this is an array of pointers, you'll need to do this to empty it out:

for (int i=0; i < num_objects; i++)
{
    delete data_temp[i];
}

delete[] data_temp;

This would deallocate the memory pointed to by each of the objects, then delete the array itself.

EDIT: I just noticed that you allocated these objects on the stack. If you want to store them like this and manage the memory yourself, you should allocate them on the heap, like this:

CPoint* A = new CPoint();
data_temp[i] = A;

...or, if you already have a CPoint on the stack and CPoint has a copy constructor...

data_temp[i] = new CPoint(A);
benwad
  • 6,414
  • 10
  • 59
  • 93
2

No. It wouldn't. The delete applied to the array of pointer will delete an array of size (in your case) an_integer.

If you actually want to deallocate the memory these pointers point to, you woud have to go through a for loop

for(int i = 0; i < an_integer; i++){
    delete data_temp[i];
}
//Then deallocate the array
delete[] data_temp;

That means that in your case, you won't end up "deleting" the A object on the stack (something that would result in undefined behavior). You will only delete an amount of memory equal to the size of a pointer on your machine multiplied by an_integer.

I'd say that in your case, an std::vector<A> would be justified and better.

JBL
  • 12,588
  • 4
  • 53
  • 84
1

No, it wouldn't. You'd have to loop through the pointer array and delete the heap-allocated objects (ie. ones allocated with a call to new) individually.

elmov
  • 286
  • 1
  • 11
  • the object isn't stack allocated, as I have done in the example to try and make the description short. It is actually a heap allocated array and then I use pointers to access it. – akash_c Aug 20 '13 at 12:38
0

You should use a typedef that's a perfect usage for them.

typedef CPoint* PCPoint;

After this treat PCPoint as array.

Pete
  • 179
  • 11