-2

The same code is giving me two different outputs when I run it in two different places. This is the code;

#include <iostream>
using namespace std;
struct test {
    int val;
};
int main() {
    test* a;
    cout << (a == NULL) << endl;
}

I get either 1 as the output:

enter image description here

or 0:

enter image description here

So, does an undefined pointer variable equals to NULL or not? What could be the reason for this variance?

Jonas
  • 83
  • 5
  • 9
    You already answered yourself: it's *undefined*. You cannot ever rely on the value of uninitialised pointer, and in fact, the mere act of reading the value of such variable can result is some nasty [nasal demons](http://catb.org/jargon/html/N/nasal-demons.html). – Yksisarvinen Jan 04 '23 at 20:49
  • (I think it's a very rare computer system where reading an uninitialized value causes a crash, but people have made systems like it before) – user253751 Jan 04 '23 at 20:51
  • 1
    There is a common misunderstanding among beginners that C++ defines some behaviour for all compilable code. This might be true for some languages but it is not true for C++. Undefined means undefined, – john Jan 04 '23 at 21:00
  • This code crashes on my machine, because `a` is uninitialized. – Eljay Jan 04 '23 at 21:05
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Jan 04 '23 at 21:26
  • 2
    Welcome to the world of C++, where there is such a thing as undefined behavior. This also goes to show you that you can never write "proof of concept" programs in C++ if the goal is to see if behavior is defined or not. Too many new C++ programmers would have written your program, and would be convinced it "worked" if they tried this at school and at home and got null as the value -- but it really doesn't work. You either know the behavior is undefined or you don't know it's undefined. Unfortunately that's the way it is -- you will learn by experience. – PaulMcKenzie Jan 04 '23 at 22:58
  • Does this answer your question? [What happens when I print an uninitialized variable in C++?](https://stackoverflow.com/questions/30180417/what-happens-when-i-print-an-uninitialized-variable-in-c) – JaMiT Jan 05 '23 at 05:40

2 Answers2

2

Without being explicitly initialized, it would only be initialized to NULL or nullptr if declared at file level ("globally") or with static.

Otherwise it is undefined. It might be NULL or it might be any other value. You are assuming it will be NULL, so the most dangerous thing that can happen is it coincidentally being NULL in any tests you run, as that will not check your assumption.

These pointer variables are initialized to NULL.

int *p;

int main() {
    // ...
}
int main() {
    static int *p;

    // ...
}
Chris
  • 26,361
  • 5
  • 21
  • 42
0

First of all it is undefined. But usually if you run it in visual studio or similar platforms, in debug mode the compiler might initialize it to null terminated pointer, but in release mode it is just "garbage" which will return 0 from the "==" comparison. Hope this helps!

dev-one
  • 7
  • 3
  • 3
    In debug mode it will probably be initialized to a nonsense value that's more likely to crash than null. – Mark Ransom Jan 04 '23 at 21:17
  • 1
    Addendum to above comment: [Magic Debug Values](https://en.wikipedia.org/wiki/Magic_number_(programming)#Debug_values). When you see one of these suckers, plus or minus a small offset, your program's usually trying to tell you something. – user4581301 Jan 04 '23 at 21:24