timeil0611
timeil0611

Reputation: 39

C++ after delete pointer

After deleting ptr, does cout << ptr print the address of int(6)?

If so, why is it garbled? I remember that delete only releases the data in the specified space, isn't it?

And I would like to ask when the delete releases space data here, is it only to release 6 or even the int type?

int* ptr = new int(6);
cout << "Address of the space pointed to by ptr: " << ptr << endl;
cout <<"the value of the space pointed to by ptr: "<< *ptr << endl;
delete ptr;
cout << ptr << endl;

Upvotes: 1

Views: 1047

Answers (4)

Well, ptr is a reference to a 4 byte in memory. int* ptr = new int(6); code assigns a memory block to ptr and writes 6 to that memory block.

delete ptr; code tells the compiler that the storage block with ref ptr is released. So on further requests for a storage block can be satisfied by the storage block with ref ptr. After delete ptr; the value of 6 is not deleted from memory with ref ptr.

On further request for memory location like in code int *newptr = new int; ref ptr can be assigned to newptr.

int* ptr = new int(6);

cout << "Address of the space pointed to by ptr: " << ptr << endl;
cout << "the value of the space pointed to by ptr: " << *ptr << endl;

delete ptr;

cout << ptr << endl;
cout << *ptr << endl;

*ptr = 7;
cout << *ptr << endl;

int *newptr = new int;

cout << "new ptr = " << newptr << endl;

Run this code for better understanding.

Upvotes: 1

anastaciu
anastaciu

Reputation: 23792

int* ptr = new int(6); reserves some memory where ptr will be pointing to, that memory will be good to store one int, 6 or any other, it cannot be used to do anything else, you can reliably store the data there and access it later.

After you delete it you tell the system that the memory is available and the program can use it for whatever else it wants. ptr may still be pointing to the same address(the value of the pointer) and you can still print it, but that memory no longer belongs to ptr, accessing that memory through it (e.g.: dereferencing the pointer), amounts to undefined behavior.

The value of the pointer(which is the address it's pointing to) normally remains unchanged until you change it yourself.

A somewhat common practise is to assign nullptr to a pointer that doesn't point to any valid memory location, that way you can easily check if it can be dereferenced or not.

Upvotes: 5

Uri Raz
Uri Raz

Reputation: 434

A pointer is a variable that contains the address of another variable, in this case ptr is initialized with the address of an anonymous int variable.

Delete releases the memory allocated for the anonymous int, but as ptr is passed by value, its value doesn't change - it still contains the same address formerly occupied by the int.

Therefore you can print the content of the variable ptr after calling delete, which will give you said address, but accessing that address (dereferencing ptr) is undefined behavior.

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234635

Once you've called delete ptr;, the value of ptr is indeterminate. (Note that the behaviour on dereferencing the pointer is undefined).

It's as if you had written

int* ptr;

without initialisation, followed by a read of the pointer with

cout << ptr << endl;

Upvotes: 0

Related Questions