Youssef Khloufi
Youssef Khloufi

Reputation: 705

Delete a pointer array without deleting the pointed objects in memory?

I would like to know if there is a way to delete a pointer array without touching the pointed objects in memory.

I'm writing a restriction routine for a HashSet I implemented a couple of days ago, so when the hash table is full it gets replaced by another double sized table. I'm representing the hash table using an array of pointers to an object (User), and the array itself is declared dynamically in my HashSet class, so it can be deleted after copying all its content to the new table using a hash function.

So basically I need to:

  1. Declare another table with a size that equals the double of the original array size.
  2. Copy every pointer to User objects from my original array to the new one applying my hash function (it gets the User object from memory and it calculates the index using a string that represents the user's name).
  3. After inserting all the pointers from the original array to the new one, I will have to free the allocated memory for the original array and replace the pointer in my HashSet class (member private userContainer) with the location of the new one (array).

The problem is that if I use delete[] userContainer to free the allocated memory for it, it will also delete every object in memory so the newly created replacement array will point to freed positions in memory!

Upvotes: 2

Views: 3571

Answers (4)

esskar
esskar

Reputation: 10970

What you describe does not sound right.
Let's say you have a class A and you create an array of As with:

A** array1 = new A*[32];

Then fill it:

for(int i = 0; i < 32; ++i)
    array1[i] = new A();

Doing a delete[] array1 does not free the elements of array1.

So this is safe:

A** array1 = new A*[32];
for(int i = 0; i < 32; ++i)
    array1[i] = new A();

A** arary2 = new A*[64];
for(i = 0; i < 32; ++i)
   array2[i] = array1[i];

delete [] array1;

for(i = 0; i < 32; ++i)
    // do something with array2[i]

Upvotes: 5

Kerrek SB
Kerrek SB

Reputation: 477600

I don't think your problem exists. Here's a baby example to show that there's nothing to worry about:

Foo * brr[10];

{
   Foo * arr[10]; 

   // This is not touching the objects!
   for (Foo * it = arr; it != arr + 10; ++it) *it = new Foo;

   std::copy(arr, arr + 10, brr);

}  // no more arr

for (Foo * it = brr; it != brr + 10; ++it) delete *it;  // fine

You can copy the pointers around freely as much as you like. Just remember to delete the object to which the pointers point when they're no longer needed.

A perhaps trivial reminder: Pointers don't have destructors; in particular, when a pointer goes out of scope, nothing happens.

Upvotes: 2

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77485

Do you know the difference between malloc/free, new/delete and new[]/delete[]? I figure that you might want to not use new[]/delete[] in your situation, as you don't want destructors to be called I guess?

Upvotes: 1

Hot Licks
Hot Licks

Reputation: 47759

In general, when you delete an array of pointers, whatever objects the pointers pointed to remain in existence. In fact, this is a potential source of large memory leaks.

But in some sort of reference-counted environment (eg, Objective-C or Qt), when you delete an array OBJECT (vs a simple [] array) then the reference counts are decremented and the objects will be deleted if the count goes to zero.

But if you're restructuring a hash table you'd better have somehow saved the pointer values before you delete the array, or else all the addressed objects will be lost. As you save them you can increment their reference counts (if you do it right).

(It would help to know what language you're dealing with, and what you mean by "array".)

Upvotes: 2

Related Questions