Home > Net >  Can a pointer returned by new have the same value as a deleted pointer returned by a previous use of
Can a pointer returned by new have the same value as a deleted pointer returned by a previous use of

Time:10-16

Will both the cout statement always print the same value?

#include<bits/stdc  .h>
using namespace std;

int main(){
    
    int* p = new int();
    *p = 54;
    cout<<p;    // it displays the address of int in heap();


    delete(p);
    p = new int[4];
    cout<<p;    // it displays the address of first block of memory, of array in heap;
    return 0;
}

CodePudding user response:

Will both the cout statement always print the same value?

No. After the delete(p) the memory allocated by the first new expression is deallocated and therefore free to be reused for a later evaluation of a new expression, but there is no guarantee at all that it will be.

That you are reusing the same pointer variable p is irrelevant. It simply stores the address of the storage location returned by the new expressions. You could have used different pointer variables to store the addresses in the same way.

It is also irrelevant that you create a int in the first new expression and a int[4] in the second. Both of the new expressions happen to return a int* which can be stored in the same variable, but nothing would change about the above if you had used simply int in both, or had used completely different types and a pointer variable of type void*.

CodePudding user response:

Will both the cout statement always print the same value?

No. The purpose of delete is to allow memory to be reused so it's possible that the next allocation returns a value just deleted.

But how new manages memory is unspecified. For example it my be managing pools of different sizes so asking for space 4 integers may not be from the same pool as 1.

It may be that on your platform that simple program will always return the same value twice. But you should regard that as entirely incidental and not something useful.

CodePudding user response:

Whilst in this simple case you may observe the same address assigned to p that is not generally the case. The allocator will allocate a block of memory on a first fit basis, and it is likely in this case that the first available block of the requisite size will be at the same address as the block just released.

If you were to change the example to:

delete(p);
volatile int* dp = new int[4] ;
p = new int[4];
cout<<p;

You will almost certainly observe that the pointer value of p is no longer the same. Moreover if you were to have:

int* p1 = new int[4] ;
int* p = new int[4];
int* p2 = new int[4];
cout << p1;
cout << p2 ;
cout << p ;
delete[] p ;
p = new int[Ox8000];
cout << p;

It is likely that the heap will be fragmented such that the second p allocation will not fit in the space released by the delete.

The delete does not remove the memory from existence, it rather marks the block as available for re-use, returning it to the heap (and coelesing it with adjacent free memory). What you are observing when you get the same pointer value is simple reuse of the space returned to the heap.

  • Related