-5

Here is my code:

int main()
{
    int *p;
    void *x;
    printf("%p\n", p);
    printf("%p\n", x);
    return 0;
}

which will print:

koraytugay$ ./a.out
0x7fff53b35ad0
0x0
koraytugay$ ./a.out
0x7fff5803fad0
0x0
koraytugay$ ./a.out
0x7fff512c9ad0
0x0
koraytugay$ ./a.out
0x7fff55213ad0
0x0
koraytugay$ ./a.out
0x7fff52dbdad0
0x0

Is there any explanation to this behaviour in the language?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • 6
    `p` and `x` are uninitialized variables and standard says using uninitialized variables leads to undefined behavior. The code doesn't make any sense printing out uninitialized variables – Gopi Jun 10 '15 at 06:48
  • 3
    Nope. No explanation with definition. Your code invokes undefined behavior. You're evaluating *indeterminate* variables in *both* cases. – WhozCraig Jun 10 '15 at 06:48
  • Is undefined behaviour. What's your question? – LPs Jun 10 '15 at 06:48
  • @WhozCraig Are you sure indeterminate values cause UB in C (as opposed to C++)? – T.C. Jun 10 '15 at 06:50
  • I don't think that it invokes an undefined behavior, just that the values are indeterminate by the standard. The implementations, however, may just extend the standard and still be standard-compliant. Uninitialized local variables, for example, in MacOS always zeroed out (as far as I remember, for security reasons), and may deceive you into thinking that this is just a general fact. It is not. It only happens to be so in MacOS. – Utkan Gezer Jun 10 '15 at 06:58
  • OK, N1570 6.3.2.1/p2. [This](http://blog.frama-c.com/index.php?post/2013/03/13/indeterminate-undefined) is a fun read. – T.C. Jun 10 '15 at 07:01
  • @Koray I edited the question title to make it more suitable. Is that ok with you? – Sourav Ghosh Jun 10 '15 at 07:10
  • @SouravGhosh Sure, it is just a question :) – Koray Tugay Jun 10 '15 at 07:17

2 Answers2

6

You asked:

In C, why is a void pointer NULL by declaration but other types contain garbage?

That is an erroneous conclusion using a small program. Uninitialized pointers in function scope get random values. You cannot rely on any pattern in their values.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    @KorayTugay well it happens in your system with your program doesn't mean that it'll always happen in any cases, any systems, with any compilers – phuclv Jun 10 '15 at 06:51
  • @KorayTugay Reorder your print statements - depending on your environment, you might then get the impression "that int pointers are always NULL" (which they are not, they are simply **uninitialized**) – Andreas Fester Jun 10 '15 at 06:51
  • @KorayTugay That is what UB is about. On my system, after I exchange the two print lines, printing `p` results in NULL. – Andreas Fester Jun 10 '15 at 06:53
  • 3
    @KorayTugay You are looking at one tree and concluding that all trees in all forests in all countries are the same. – kaylum Jun 10 '15 at 06:55
  • @AlanAu Well actually I am asking other foresters about an observation I made to get answers from experienced people instead of go look for 12341 more trees. – Koray Tugay Jun 10 '15 at 06:56
  • @KorayTugay Those experienced people have given you the answer. But you keep saying "but it's not the case on *my* system". – kaylum Jun 10 '15 at 06:57
  • @KorayTugay if you reverse the sequence of the two pointer definitions you'll still get the same program output, showing that a `void*` is not initialised to `NULL`. As a local variable, it's not inintialised unless *you* do so. – Weather Vane Jun 10 '15 at 06:58
  • @WeatherVane For me even if I reverse the sequence, output is same. – Koray Tugay Jun 10 '15 at 07:00
6

I think, the C11 standard is quite clear in this regard. Referring chapter 6.7.9, paragraph 10,

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

Now, an indeterminate value is , well, indeterminate (which you're referring to as garbage and / or NULL here). You cannot really know what is going to be there.


EDIT:

Just to clarify, as per the comment,

"But the void *p seems to be NULL always"

Right. It seems. It's nothing guaranteed (specified), as far as C standard is considered.

Just a note: Prefer int main(void) over int main(). The former is recommended.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261