0

Case 1

#include <iostream>
using namespace std;
int main() {
    int n = 1;
    // int & r = n;
    int * p;
    cout << &n << endl;
    cout << p << endl;
    return 0;
}

Output 1

0x7fffffffdc94
0

Case 2

#include <iostream>
using namespace std;
int main() {
    int n = 1;
    int & r = n;
    int * p;
    cout << &n << endl;
    cout << p << endl;
    return 0;
}

Output 2

0x7fffffffdc8c
0x7fffffffdd90

In Output 2, the pointer p just pointed to the address followed int n. Shouldn't an unintialized pointer point to some random places? Why adding a reference declaration influences the address of p pointed to?

qiao shao
  • 3
  • 1
  • 3
    `cout << p << endl;` is undefined. The output of this code could be anything – 463035818_is_not_an_ai Jul 19 '22 at 09:29
  • 2
    "Shouldn't an unintialized pointer point to some random places?" no. The value is indeterminate. There is no way to read the value without invoking undefined behavior – 463035818_is_not_an_ai Jul 19 '22 at 09:30
  • 1
    "non initialized variables hold random values" is a myth. It is wrong. Nothing is random here, what you observe depends on implementation details of your compiler, details that wouldn't matter if your code would have defined behavior – 463035818_is_not_an_ai Jul 19 '22 at 09:31
  • 1
    what compiler and what compiler options are you using? Using a different compiler or different flags can result in completely different output. – 463035818_is_not_an_ai Jul 19 '22 at 09:35
  • 1
    Your code has undefined behaviour because you access the value of an uninitialised variable. Printing out 'some random places' is one thing that might happen when your code has undefined behaviour but it's not the only thing that can happen. It could crash, it could print zero, it could whistle Dixie, anything. – john Jul 19 '22 at 09:39
  • `p` is uninitialised. So accessing its value (which happens in order to print it) gives undefined behaviour. Behaviour that changes (e.g. to result in different output) due to seemingly unrelated modification of your code is one possible manifestation of undefined behaviour. – Peter Jul 19 '22 at 09:46
  • Thanks everyone! My original doubt came from a function on page 400 of C++ primer plus, which used an undefined pointer to create new storage. I just want to clarify if it is an unsafe behavior. – qiao shao Jul 19 '22 at 10:18
  • Why it matters that it's not "a random value" - if it was really a random value, like `reinterpret_cast(rand())`, the pointer variable would keep that random value until somebody wrote a new value to it. But with Undefined Behavior, it is entirely possible that you see a _different_ random value everytime you look. And the 5th time you look, the program could crash - no guarantee that the crash will happen the first time you look. – MSalters Jul 19 '22 at 10:59

1 Answers1

2

Shouldn't an unintialized pointer point to some random places?

No, an uninitialized pointer points nowhere. It has an indeterminate value. Trying to read and/or print this indeterminate pointer value as you are doing in

cout << p << endl;

has undefined behavior. That means there is no guarantee whatsoever what will happen, whether there will be output or not or whatever the output will be.

Therefore, there is no guarantee that changing any other part of the code doesn't influence the output you will get. And there is also no guarantee that the output will be consistent in any way, even between runs of the same compiled program or multiple compilations of the same code.


Practically, the most likely behavior is that the compiler will emit instructions that will simply print the value of whatever memory was reserved on the stack for the pointer. If there wasn't anything there before, it will likely result in a zero value. If there was something there before it will give you that unrelated value.

None of this is guaranteed however. The compiler can also just substitute the read of the pointer value with whatever suits it or e.g. recognize that reading the value has undefined behavior and that it may therefore remove the output statement since it can not be ever reached in a valid program.

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • Thanks! My original doubt came from a function on page 400 of C++ primer plus, which used an undefined pointer to create new storage. I just want to clarify if it is an unsafe behavior. – qiao shao Jul 19 '22 at 10:21
  • 1
    @qiaoshao I hope that is just a misunderstanding or you are leaving out information here. Otherwise the book is telling you nonsense. You can't use an uninitialized pointer to create new storage. It is no just unsafe. It simply doesn't make any sense. – user17732522 Jul 19 '22 at 10:30