This is because pPointer
is pointing to a local variable in func()
. This area of memory is only valid inside of the func()
method. After func()
exits, the memory that pPointer
points to is free to be re-used, and appears to be used by cout
.
If you want to persist this memory, you should either save it by value, or allocate the memory dynamically pPointer = new int(num)