-4

I couldn't find in the C standard, if uninitialized pointers evaluate to true, false, or result in undefined or implementation defined behavior? For example:

{
    int *p;
    if (p)
     do_sth();

}
user2736738
  • 30,591
  • 5
  • 42
  • 56
cmutex
  • 1,478
  • 11
  • 24
  • UB, of course.­ – iBug Jan 27 '18 at 07:51
  • [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/q/11962457/995714) – phuclv Jan 27 '18 at 07:54
  • 3
    Context matters! If the pointer is defined inside a function (as it seems to be since the next statement is an `if` testing it), the result is undefined behaviour. If the pointer is defined outside any function, then it is default initialized to a null pointer and will compare false. – Jonathan Leffler Jan 27 '18 at 07:54
  • And if it is in the function and has had its address taken, it resides probably in memory and its contents are indeterminate and value can change at any instance of use but it would behave as if it was true or false at any given moment. – Antti Haapala -- Слава Україні Jan 27 '18 at 07:57
  • Can someone point to me the specific section in the C standard? I can't find it. And no, it is not a duplicate question. – cmutex Jan 27 '18 at 08:02
  • And no, using uninitialized local variable is not an undefined behavior (e.g. adding two initialized int variables - it is just some garbage value). De-referencing an uninitialized pointer is undefined. – cmutex Jan 27 '18 at 08:05
  • Why would there be a section in the standard that defines the boolean state of a pointer? There is no section that defines the truth of three-sevenths either. – Martin James Jan 27 '18 at 08:34
  • @user3124390 no, simply reading any uninitialized values invokes UB, no need to dereference the pointer, because there are many architectures that don't allow that. Please read the duplicate questions – phuclv Jan 27 '18 at 15:26

1 Answers1

1

In your sample code it's undefined behavior to access any uninitialized values in C, regardless of its type (except for unsigned char).

Since a pointer is not a unsigned char type, the behavior is undefined.

Note for implicit initialization, like putting the definition in file scope, or defining it with static storage class specifier, causes the object to be zero-initialized by default.

For more information, see (Why) is using an uninitialized variable undefined behavior?

iBug
  • 35,554
  • 7
  • 89
  • 134
  • @AnttiHaapala I know what you mean. – iBug Jan 27 '18 at 07:53
  • The value obtained from accessing an uninitialized `unsigned char` is unspecified, even if the access does not result in undefined behaviour. An `unsigned char` defined at file scope (or inside a function with storage class `static`) is initialized to 0. – Jonathan Leffler Jan 27 '18 at 07:56
  • @JonathanLeffler Your comment was slower than my awareness of those missing points :) – iBug Jan 27 '18 at 07:57
  • It's what comes of repeating myself without using copy'n'paste :D – Jonathan Leffler Jan 27 '18 at 07:57
  • @JonathanLeffler: When using gcc, the result of accessing an uninitialized `unsigned char` may be worse than Unspecified, even when the access is done using a character pointer. Under gcc, reading all bytes of an uninitialized object and storing all those values into the bytes of another uninitialized object may cause gcc to treat the second object as uninitialized even though all of its bytes have been written. – supercat Jan 29 '18 at 19:09