0

I'm facing an issue. I wrote the following programme:

void main() {

int *ptr;
ptr=new int;


cout<<ptr<<endl;
cout<<&ptr<<endl;


delete ptr;

// ptr= NULL;

cout<<ptr<<endl;
cout<<&ptr<<endl;

} The output of the programme is:

0x00431810 0x0018FF44 0x00431810 0x0018FF44

"&ptr" would definitely be the same because it is ptr's own address. But 1st and third line of output clearly shows that "ptr" is pointing to the same memory location even after the "delete" is called. Why? If this is the case why do we have "delete" and not just rely on NULLING the pointer. I know that more than one pointers can point to a memory location but yeah "delete" is useless. No ? That's so confusing. please help. I've an exam tomorrow.

user3054062
  • 97
  • 1
  • 3

4 Answers4

3

delete ptr does not change the value of the pointer, but it does free the memory.

Note that dereferencing ptr after freeing the memory would trigger undefined behaviour.

hyde
  • 60,639
  • 21
  • 115
  • 176
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

When you call new, you are asking the computer to allocate some memory on the heap and returns a pointer to where that is on the heap.

Calling delete does the opposite in that it tells the computer to free the memory the pointer is pointing to from the heap. This means that it can be reused by your program at a later stage.

However, it does not change the value of the pointer. It is good practise to set the pointers value to NULL. This is because, where the pointer is now pointing to could contain anything (the computer is free to reuse this memory for something else). Were you to use this pointer, it is undefined what will happen.

If you just change the pointer to NULL, then all that does it forget where you allocated the memory. Because C++ is not a managed language, it won't detect you don't have any pointer to that memory, so the memory will become lost as you can't access it (you don't know where it is) but the computer can't reuse it (it doesn't know you are done with it).

If you use a managed language like C# or Java, when there are no pointers to something on the heap, the computer frees it (known as Garbage Collection) but this comes with performance overheads so C++ leaves it up to the programmer.

T. Kiley
  • 2,752
  • 21
  • 30
1

pointer doesn't know if the memory is valid or corrupted. You can make pointer to point at any address that you can address (that will fit into pointer). For example on my machine pointer has 8 bytes size so I can do

int main(int argc, char** argv) {

int *ptr;
ptr=new int;

cout<<ptr<<endl;
cout<<&ptr<<endl;

delete ptr;
 //ptr= NULL;

cout<<ptr<<endl;
cout<<&ptr<<endl;

ptr = (int*) 0xffffffff;            
cout<<ptr<<endl;                    //prints 0xffffffff
ptr = (int*) 0xffffffffffffffff;    
cout<<ptr<<endl;                    //prints 0xffffffffffffffff
ptr = (int*) 0xffffffffffffffff1;   
cout<<ptr<<endl;                    //prints 0xfffffffffffffff1  truncated
ptr = (int*) 0xffffffffffffffff321; 
cout<<ptr<<endl;                    //prints 0xfffffffffffff321  truncated
return 0;
}

When you are using new it will allocate memory at some address on the heap and return you a pointer to this address.

void* operator new (std::size_t size) throw (std::bad_alloc);

Pointer and memory are two distinguished things. In particular you can even specify an address which you want to use for allocation by hand(using placement new syntax):

int* ptr = new (0xff1256) int; 
//correct if the memory at 0xff1256 is preallocated
//because placement new only creates object at address
//doesn't allocate memory

"ptr" is pointing to the same memory location even after the "delete" is called. Why?

In the same way delete ptr only deallocates memory, leaving the pointer intact (this is a good practice though to assign NULL to a pointer immediately after call to delete). It only deallocate the memory, it is not interested in your pointer but in the address where to free memory. Allocating memory or deallocating it is done by operating system (i.e.the memory is being marked as allocated).

4pie0
  • 29,204
  • 9
  • 82
  • 118
0

From the documentation,

Deallocates the memory block pointed by ptr (if not null), releasing the storage space previously allocated to it by a call to operator new and rendering that pointer location invalid.

That means, delete marks the location to be reused by any other process for any other purpose. It never says that, it will initialize the memory block to any random value, so that the earlier stored data can never be accessed again. So, there is always the possibility of having your data even after the call to delete. But, again, it is just one of the possibility of Undefined Behavior. Thats' why coders, make the pointer NULL after deallocating it.

Abhineet
  • 5,320
  • 1
  • 25
  • 43
  • Not by "any other process" (in common PC OSes), processes have their own virtual address spaces. – hyde Dec 02 '13 at 05:54