How is the destination that an uninitialized pointer in c points to determined?
c, pointers
Solution
If this pointer is defined outside of all functions (or is `static`), it will be initialized to `NULL` before `main()` gets control.
If this pointer is created in the heap via `malloc(sizeof(sometype*))`, it will contain whatever happens to be at its location. It can be the data from the previously allocated and freed buffer of memory. Or it can be some old control information that `malloc()` and `free()` use to manage the lists of the free and allocated blocks. Or it can be garbage if the OS (if any) does not clear program's memory or if its system calls for memory allocation return uninitialized memory and so those can contain code/data from previously run programs or just some garbage that your RAM chips had when the system was powered on.
If this pointer is local to a function (and is not `static`), it will contain whatever has been at its place on the stack. Or if a CPU register is allocated to this pointer instead of a memory cell, it will contain whatever value the preceding instructions have left in this register.
So, it won't be totally random, but you rarely have full control here.
Problem
I know that if a pointer is declared in C (and not initialized), it will be pointing to a "random" memory address which could contain anything. How is where it actually points to determined though? Presumably it's not truly random, since this would be inefficient and illogical.