A long time ago I used to program in C for school. I remember something that I really hated about C: unassigned pointers do not point to NULL.
I asked many people including teachers why in the world would they make the default behavior of an unassigned pointer not point to NULL as it seems far more dangerous for it to be unpredictable.
The answer was supposedly performance but I never bought that. I think many many bugs in the history of programming could have been avoided had C defaulted to NULL.
Here some C code to point out (pun intended) what I am talking about:
#include <stdio.h>
void main() {
int * randomA;
int * randomB;
int * nullA = NULL;
int * nullB = NULL;
printf("randomA: %p, randomB: %p, nullA: %p, nullB: %p\n\n",
randomA, randomB, nullA, nullB);
}
Which compiles with warnings (Its nice to see the C compilers are much nicer than when I was in school) and outputs:
randomA: 0xb779eff4, randomB: 0x804844b, nullA: (nil), nullB: (nil)
Actually, it depends on the storage of the pointer. Pointers with static storage are initizalized with null pointers. Pointers with automatic storage duration are not initialized. See ISO C 99 6.7.8.10:
And yes, objects with automatic storage duration are not initialized for performance reasons. Just imagine initializing a 4K array on every call to a logging function (something I saw on a project I worked on, thankfully C let me avoid the initialization, resulting in a nice performance boost).