For example, we have the following simple code:
#include "stdio.h"
int main() {
int* pointer_to_check;
printf("%x\n", pointer_to_check);
return 0;
}
With gcc, we will get zero at output (as for me, normal behavior, because pointer isn't associated with memory), but clang gives us a real address, which can be accessed without "segmentation fault".
I have two questions:
- What is the normal behavior according to the C standard (and C++)?
- Why does this happen?
pointer_to_check
to check is uninitialized. It points to some "random" location.Accessing uninitialized variables leads to Undefined Behavior which means that anything can happen.
Also, pointers should be printed using the
%p
format specifier.Anything can be the output. It is Undefined. You shouldn't rely on this behavior.