1

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;
anastaciu
  • 23,467
  • 7
  • 28
  • 53
timeil0611
  • 39
  • 1
  • 1
  • 7
  • 1
    delete command only free the memory in use by that pointer. So it will only release the value 6 not the int type. – Talal02 May 08 '21 at 09:11
  • 3
    `delete ptr` releases the memory that `ptr` points at (which was created as `new int (6)`). It doesn't change the value of that pointer. So `cout << ptr` will print the same value (of the pointer) as it did before doing `delete ptr`. If you were to print `*ptr` after having done `delete ptr` the behaviour is undefined (which means it COULD print the value `6`, it COULD print "garbage", but the result COULD also be reformatting your hard drive - undefined behaviour means any outcome is possible). – Peter May 08 '21 at 09:17
  • A pointer is nothing else than a number of a storage cell. In the ancient K&R C, there wasn't made a big difference between `int` and pointer. Later, that was re-fined, of course, as confusing pointers and values is an often mistake which is easily done but shows hard consequences. However, a pointer is a pointer. Deleting the contents (where the pointer points to) is one thing but it doesn't change the pointer itself. But, if you would access the contents of the pointer after delete - that would be Undefined Behavior. – Scheff's Cat May 08 '21 at 09:20
  • After you delete `ptr` it is undefined what will happen if you continue to use it. – user207421 May 08 '21 at 09:25
  • https://stackoverflow.com/q/44182049/1918193 – Marc Glisse May 08 '21 at 12:15
  • @Peter -- the standard does not require that `delete ptr;` leave the value of `ptr` unchanged. Granted, changing it would, ordinarily, be silly, and nobody does it, but it's allowed. – Pete Becker May 08 '21 at 13:18
  • @Peter But when I `delete` the `ptr` is different from the `ptr` before `delete`, I don’t know why – timeil0611 May 08 '21 at 17:08

4 Answers4

5

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Accessing the *value* of `ptr` after `delete ptr` (e.g. to print it) doesn't cause undefined behaviour, since its value remains unchanged. Dereferencing it (i.e. treating the value of `ptr` as if it is the address of a valid object, which it no longer is) causes undefined behaviour. – Peter May 08 '21 at 11:41
  • @anastaciu But when I `delete` the `ptr` and print it. It is different from the `ptr` before `delete`, I don’t know why – timeil0611 May 08 '21 at 17:09
  • @timeil0611, it can be different, the pointer after `delete` is not mandated to retain its original value, VS is not wrong, it's unusual though. Other compilers don't bother changing the pointer value. – anastaciu May 08 '21 at 20:40
  • @Peter, that may not always be the case, the pointer value may be changed by the compiler, MSVC is in the habit of doing it https://stackoverflow.com/q/33374483/6865932 – anastaciu May 08 '21 at 21:14
1

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.

Hamaad
  • 88
  • 8
0

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;
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    Not true. `delete ptr` causes the object(s) that `ptr` points at (e.g. `*ptr`) to no longer be a valid object. It doesn't change the value of `ptr`, so it doesn't cause the value of `ptr` to become indeterminate. – Peter May 08 '21 at 11:45
0

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.

Uri Raz
  • 435
  • 1
  • 3
  • 15