13
Anakata
4y

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.
  • 1
    nullptr, you animal ;-)
  • 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!
  • 1
    @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.
  • 2
    @Fast-Nop And code mixed with multiple compilation units and constructors that operate on globals lists that init part of the params of items in that list...

    Other devs: It works on Linux.

    me: It was luck of the draw that it worked. It changed the order on Windows. Why are you init-ing params that the OS zeros out anyway?

    Other devs: I don't know...

    me: I am taking out the constructor. This code needs a redesign at some point.

    Other devs: okay
Add Comment