-2

I'm having difficulty finding an answer on how to specifically perform this operation properly.

I'd like to better understand different ways to delete new memory allocated on the heap, especially in the instance of a two-D array.

For my example: I have an array of size 5 on the stack consisting of int pointers (int *d[5]). I initialize each of these int pointers in a loop to create and point to an int array of size 8 on the heap (d[i] = new int[8]). I have now essentially created a two-D array (d[j][k]).

My question is what is the syntax for deleting only the individual arrays (int[8]) on the heap?

I currently have it as this, but when debugging it appears the arrays are not deallocated after it is performed...

for(int i = 0; i < 5; ++i) delete d[i];

Should it be "delete [] d[i]" or just "delete [] d" or some other variation? Also, is there a way to delete the individual elements of the int[8] arrays? If anyone could concisely explain the syntax and their differences that would be super helpful. Thank you!

MajinMilad
  • 13
  • 3
  • *I initialize each of these int pointers in a loop to create and point to an int array of size...* - that was the first mistake. `std::vector>` solves nearly every problem you're bound to run in to, including allowing for in-sequence removal (though potentially expensive). [RAII](https://en.cppreference.com/w/cpp/language/raii) is what's for dinner. – WhozCraig Oct 20 '18 at 22:06

2 Answers2

1

If you allocated arrays via d[i] = new int[8], then you must delete them via delete[] d[i]. There's no way to deallocate individual elements of such an array without deallocating the whole thing.

0

you mentioned that you are allocating the inner arrays within a loop. which I believe it looks something like this.

int*d[2];
for (int i = 0; i < 2; i++)
{
    d[i] = new int[3];
}

Notice that d[2] contains just pointers to the arrays. so in order to delete this array, you have to iterate through each set of array pointers and call delete[] d[i];

for (int i = 0; i < 2; i++)
{
        delete[] d[i];
}

As an additional note, it would be very advantageous to know how your IDE tries to detect memory corruptions.

for example in the visual studio (In DEBUG mode)

  • 0xCD means Allocated memory via malloc or new but never written by the application.
  • 0xCC means uninitialised variables.
  • 0XDD means memory has been released with delete or free.
  • 0xFD means fence memory and acts as a guard. used for detecting indexing arrays that go out of bounds.

with that in mind lets see if we can make sense of what the IDE is doing when the above code is executed.

When the d array is declared int*d[2]; the memory layout looks like the following; enter image description here notice that d array has two elements but none of those have initial values so they are assigned to the 0xCC

lets see what happens after we do d[i] = new int[3]; enter image description here notice that d array now has two elements each element contains an int array. the values you see are the address of the pointers to the array we allocated the memory for.

since we know what the addresses are we can look into the memory and see whats happening in there when allocating and deleting the each array.

for example after we allocate our second int array within the for loop, the memory location would look something like this;
enter image description here notice that all the array element has 0xCD with ending of 0xFD.This would indicate in my IDE that the memory allocated and has a fence guards around it.

lets see what happens when the d[2] is deleted. enter image description here

Yucel_K
  • 688
  • 9
  • 17
  • I see thank you. "delete [] d[i]" will free the memory in the heap (int[3]) which d[i] points to, correct? It does not delete the actual pointer d[i] – MajinMilad Oct 22 '18 at 22:30
  • @MajinMilad that's is correct. think of it like this. the pointer is a variable that just holds an address value of the object it's supposed to be pointing at. of course it can be a null ptr if its point to nothing or it can be an invalid pointer if it's pointing to an already deleted object. – Yucel_K Oct 22 '18 at 22:46
  • @MajinMilad you may find more answer here https://stackoverflow.com/questions/23621677/what-happens-to-the-pointer-itself-after-delete – Yucel_K Oct 22 '18 at 22:47
  • excuse me but I have a class `MC` with 2 members, an `enum` object and a `char`. I have a pointer of that class as a map and I do `MC* map = new MC[1050]`. I treat it as 2d array and it works fine, until the player dies, the game ends and the time has come for the map to be `delete`ed, `delete[] map` throws `Invalid address specified to RtlValidateHeap([some address], [some address])` and the program crushes, any suggestions? – platinoob_ Sep 08 '20 at 11:29