Question

My code:

#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
    int * p;
    p = new int[5];
    p[0] = 34;
    p[1] = 35;
    p[2] = 36;
    p[3] = 44;
    p[4] = 32;
    cout << "Before deletion: " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << p[i] << endl;
    }

    delete[] p;
    cout << "After deletion: " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << p[i] << endl;
    }
}

If delete[] is supposed to delete all the memory blocks created by the new then why does only the first two elements get deleted here?

This is the output:

Before deletion: 
34
35
36
44
32
After deletion: 
0
0
36
44
32

One possible reason I could think of is that it removes the first two which some how makes the memory management think that the whole block is free to be reallocated again when needed. Though this is a complete guess on my part and I don't know the real reason.

Was it helpful?

Solution 2

It is not the first two items deleted, but all of them. That memory area could now be reused by further new operation, but there is no guarantee what is left in that memory area.

It is completely possible that you would see different results for different machines, subsequent runs on the same machine, different compiler versions and so on. Just do not rely on it.

However, you are accessing a dangling pointer afterwards which is Undefined Behavior (shortly UB).

Just in case, if you call delete without the square brackets, all the memory is de-allocated, but destructors may not be called. That is also UB even though you do not have custom types in this case, just ints.

In your case, it is exactly the same UB behavior with delete as well as delete[], respectively. That is why you see the same results in both cases for your current runs, but then again, even that is not guaranteed.

Ideally, you would need to set them to zero, too, but really, you should consider using a smart pointer, like unique_ptr.

OTHER TIPS

There's no need to clear (as in change the value) of a deleted element, it only means that the allocator is free to use the space it occupied for whatever it wants, and that accessing the values results in undefined behaviour.

You have experienced a really nasty result of this, which is that some values remain unchanged, while successive runs of the same program might (and you should assume that will) have different results.

Q: If delete[] is supposed to delete all the memory blocks created by the new then why does only the first two elements get deleted here?

A: The conclusion is incorrect. Try the following:

#include <iostream>

struct A
{
   A() : a(0) {}
   ~A() { std::cout << "Came to ~A(). a = " << a << std::endl;}
   int a;
};

int main()
{
   A* ap = new A[5];
   for (int i = 0; i < 5; ++i )
   {
      ap[i].a = i;
   }

   delete [] ap;
}

I get the following output:

Came to ~A(). a = 4
Came to ~A(). a = 3
Came to ~A(). a = 2
Came to ~A(). a = 1
Came to ~A(). a = 0

A pointer is not magic. It is effectively an unsigned integer of machine word size which refers to a memory address. Changing the value of a memory location from whatever it is to 0 is not deleting anything. When you call delete or delete [] all you've done is told the memory allocator that you are done with that memory and it is free to do (or not do) whatever it wants with that memory. After you call delete, what might be in that memory is undefined, you have no right to expect it to contain anything in particular, and you have no right to access it. Accessing memory that has been freed is like entering an apartment that you moved out of that you still have a key to; you might get away with it or you might get an access violation.

For types with destructors, the memory allocator will call the destructor for each object in the block of memory before reclaiming the memory block. ints have no destructor, so there's nothing extra for it to do.

delete[] p releases the memory held by p, but doesn't (necessarily) overwrite it. The old data will remain there indefinitely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top