-1

Given the following code snippet:

#include <iostream>
using namespace std;
int *pPointer;

int func()
{
    int num;
    num = 25;
    pPointer = &num;
}

int main()
{
    func();
    cout << *pPointer << endl;
    cout << *pPointer << endl;
    return 0;
}

Can anyone tell me if I duplicate the following line:

cout << *pPointer << endl;
cout << *pPointer << endl;

Why I receive 25 (as expected) but then the next value is 0 (NULL)?

Wouldn't the value of int=25 still remain on the stack? What is it about cout that changes the value of pPointer? Am I missing something about my understanding about scope and the stack? (I'm a Java guy, so this was surprising).

Deanie
  • 2,316
  • 2
  • 19
  • 35
gcraig
  • 103
  • 8
  • 1
    Your code has undefined behavior everywhere. `func()` needs to return an `int`. And dereferencing the pointer after `func()` exits is also undefined. – Praetorian Apr 17 '14 at 19:19
  • 2
    The explanation in @GSerg 's link is the best answer. – jliv902 Apr 17 '14 at 19:21
  • Thank you *everyone* I wish I could mark you all as answers. I knew it had to do something with scope and undefined behavior, but thank you for the clarification. Code on! – gcraig Apr 17 '14 at 19:25
  • Praetorian -- I see that now, yikes. Thanks. – gcraig Apr 17 '14 at 19:28

5 Answers5

2

num does not exist once the function func() terminates.

You take its address when it exists, then attempt to print the value at an address that is no longer valid.

Anything can happen.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • ah so at the time I cout the num (via the dereferenced pointer), I have 25, but since func() has already terminated, the next invocation of cout prints a pointer that is undefined? or null depending upon the compiler's implementation? thank you for your insight. – gcraig Apr 17 '14 at 19:20
  • Both the first and the second time access an address that is no longer valid. Why the value is different on both accesses is anybody's guess (I'll guess the library code for `printf()` clobbered the value there). – pmg Apr 17 '14 at 19:28
2

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)

Velox
  • 254
  • 1
  • 5
1

As it is undefined behavior, there is no num outside of func scope.

Dabo
  • 2,371
  • 2
  • 18
  • 26
1
cout << *pPointer << endl;

here *pPointer is pointing to a local variable of some other function which already returned so the address to which it is pointing is out of scope so the behaviour is undefined. it may print the right value assigned to num or may not.

LearningC
  • 3,182
  • 1
  • 12
  • 19
0

Why I receive 25 (as expected) but then the next value is 0 (NULL)?

What you mention as expected is actually not expected!

num becomes out-of-scope as soon as func() returns. Although pPointer is valid (as it is global), *pPointer is not, because it is pointing to a out-of-scope memory.

Arun
  • 19,750
  • 10
  • 51
  • 60