12
Anakata
5y

Uninitialized pointer is not the same as NULL pointer !

Uninitialized pointer is not the same as NULL pointer !

Uninitialized pointer is not the same as NULL pointer !

Uninitialized pointer is not the same as NULL pointer !

Comments
  • 3
    If it's a global pointer, it is actually a null pointer if NULL is defined as 0, which it is everywhere.

    An uninitialised pointer from the stack or in malloc'ed memory is undefined behaviour if dereferenced, so the compiler can do anything. Usually optimising large parts of the program away to have still nonsensical behaviour but with maximum performance.
  • 2
    @Fast-Nop even that's not entirely true. I've worked on a platform that had a ram-wiping sleep cycle. The ram was turned off to save power.

    When the device would wake up, all uninitialized pointers were not assigned any new value so they would contain garbage.

    This was however with an older version of GCC (5.4?), so maybe that wasn't playing nice.
    Still, always initialize!
  • 0
    @Geoxion Well yeah, if the hardware wipes out memory underneath the program, nothing works anymore. What I meant was that global non-initialised variables in C/C++ are initialised to 0 at program start so that no explicit initialisation is required. Once execution reaches main(), this must have been done, or the runtime implementation is buggy.
Add Comment