2

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!

1
  • Sorry for not mentioning the programming language. It is C++ Commented Nov 27, 2011 at 21:42

4 Answers 4

5

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic, can i do now : array1 = array2, so my class member point now to the valid array in memory?
yes, after you call delete [] array1, you can set array1 = array2 and you are done, until it is full again!
2

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".)

1 Comment

Sorry for not mentioning the programming language. It is C++
2

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.

1 Comment

Pretty easy to pretend arr dies, just scope it. ;)
1

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?

1 Comment

I want to only free the allocated space for the original array and still have objects in memory as i have already inserted the required pointers to get them in the replacement array. And i do not know the difference between malloc/free, new/delete and malloc/free, new[]/delete[], but i guess that new/delete and new[]/delete[] are based on malloc/free and that i might need to use malloc/free to reach my aim.

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.