0

Here is a simple of example of a question I have: I create a pointer of integer (value 5), I print the pointer (hence the address) of the memory case it points to, as well as its content (5).

Then I delete that pointer, which is supposed to delete the content in the memory case that has the address of the pointer. But then when I print a and its content, as expected the address still exists. Nevertheless the content remains the same (5) as if a was not deleted... could you explain me? :-)

#include <iostream>
int main()
{
    int * a = new int(5);
    std::cout<< a << std::endl;
    std::cout<< "--------" << std::endl;
    delete a;
    std::cout<< a << std::endl;
    std::cout<< *a << std::endl;

    // end of my main
    return 0;
}

result:

0x7fff28403a90
--------  
0x7fff28403a90
5
Aurélie JEAN
  • 195
  • 1
  • 1
  • 7

1 Answers1

0

It may or may not print the same value after delete operation. So simply what you are observing is an undefined behavior.

Steephen
  • 14,645
  • 7
  • 40
  • 47