0

I made a 2D array on the heap of some objects:

Step (1)

Obj **arr = new Obj *[n];

for (int i=0;i<n;i++)
{
    arr[i] = new Obj[n];
}

// so this creates 2D arr[n][n]...then when I delete it:

Step (2)

for (int i=0;i<n;i++)
{
    delete [] arr[i];
}
delete [] arr;

So I'm still not sure what this delete does. Does it run the destructor of Obj and flag the OS telling it this is now available memory.

Now what I REALLY do not understand is that when I do Step (1) again (after I deleted), I get these objects initialized to weird values, yet this doesn't happen the first time I do it (all zero-initialized). Did I just get lucky the first time?

trincot
  • 317,000
  • 35
  • 244
  • 286

2 Answers2

0

Your example lacks the declaration of Obj.

  • new[] allocates memory and calls the constructor of each element
  • If the constructor does not alter memory, you will see some random values - maybe zeros.
  • delete[] calls the destructor of each element previously allocated with new[] and deallocates the memory, finally.
  • In a debugging compilation the memory might be filled with some bytes indicating the deallocation.
  • Doing new[] right after the deallocation might show indicator bytes.
  • Yeah Obj is just a random class with some pointers and other simple fields. When I gdb the Step(2) for loop, I figure out that after delete [] obj[0] I can still print obj[0], and obj[0][0], except there are some 'funky' values in some fields – rottweilers_anonymous Nov 14 '13 at 23:42
0

AFAIK, the following code will NOT give you weird values, no matter how many times you repeat deleting and newing.

#include <iostream>
using namespace std;

class Foo
{
public:
    Foo(): val(-2) { cout << "ctor" << endl; }
    ~Foo() { cout << "dtor: " << val << endl; }

private:
    int val;
};

int main()
{
    Foo **arr = new Foo *[2];
    for (int i = 0; i < 2; ++i)
        arr[i] = new Foo[2]();    // <- for builtin type, () is required to initialized to zero.

    for (int i = 0; i < 2; ++i)
        delete [] arr[i];
    delete [] arr;
    return 0;
}

Relevant post: Operator new initializes memory to zero

As to what happens to pointers after you delete them, please see this post: C - What happens to an array of pointers when the array is freed?

Community
  • 1
  • 1
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
  • Am I missing something? I understand this but how does it relate to my question? – rottweilers_anonymous Nov 14 '13 at 23:51
  • @JoshDM Sorry for the confusion. Could you please post a working example of your code that produces the `weird` init value? I was trying to express that if you use the above code, you will not get weird value anytime you delete and new. – gongzhitaao Nov 14 '13 at 23:53
  • I can't post the actual code unfortunately. I got it working fine, but I really don't understand why when I debug the deletion loop I can still access arr[0],arr[1],arr[0][0], ... Is this just a quirk of the debugger or is something not being deleted? – rottweilers_anonymous Nov 15 '13 at 00:08
  • Thanks for the second link. So I can read Obj[0..size] (sometimes). So why is some memory I try access, say Obj[maxSize + 1] considered invalid by the debugger? Shouldn't memory also be 'invalid' if I deleted it. – rottweilers_anonymous Nov 15 '13 at 00:14
  • @JoshDM Actually, after you delete or free a pointer, the location is still valid, the location is still accessible by the pointer if you didn't set it to other value or NULL. It's still available for read/write with undefined behaviour. They might or might not be reused, if it is, you are in trouble. It's just luck if you get your old value. Memory are always valid :) – gongzhitaao Nov 15 '13 at 00:20