Why not initialize all variables to zero?

By: Jukka Larja (roskakori2006.delete@this.gmail.com), March 28, 2020 8:25 am
Room: Moderated Discussions
Anon (no.delete@this.spam.com) on March 28, 2020 12:01 am wrote:
> Jukka Larja (roskakori2006.delete@this.gmail.com) on March 27, 2020 10:14 pm wrote:
> > I don't really get this argument. We have for example std::string
> > and std::vector that are initialized to known
> > state.
>
> std::string and std::vector are not the best examples of easy to debug code.

Why? We don't actually use them, so I hardly ever try to debug them.

> > What makes an int or float so special that they shouldn't be? Compilers could always add a non-standard
> > compliant switch to skip initialization, or there could
> > be additional types that don't initialize to zero, if
> > top performance is needed and code is too complex for compiler to optimize redundant initializations.
>
> I think there are two different concepts being discussed here, the first is security,
> the second is code easy to debug, by initializing to zero one solve the security problem,
> information is less likely to leak through bugs, but, it also make finding those bugs
> harder since zero may be a valid value while a random value may not.

If someone creates a C++ class or struct that allocates memory and intentionally leaves things uninitialized, that's his problem. Everyone probably agrees that if you have a something like a string, vector or map, it would be pretty weird to require user calling some init() before using them. An empty string or empty container is what one expects by default, regardless of whether that makes sense in particular program. So why can't I expect that int and float would be zeros, a pointer NULL and a char NUL by default?

I understand the historical reasons, but considering the speed of change especially in C++, I'm really confused why we can't get basic types initialized to defined values by default.

> > As far as I can tell, nothing should be broken by initializing to zero instead
> > of random value. Why must the default still be not to initialize?
>
> The perfect world is to not allow the use of uninitialized variables because this is very
> likely a bug, so, if the compiler gives compiler errors when uninitialized variables are
> used the programmer will take a few seconds to find and fix the problems and the program
> will be more secure and less buggy, but that's almost impossible with C/C++.

If compiler heuristics about uninitialized variables[1] worked better (preferably detecting all cases), that would be fine by me. Unfortunately, they don't. We have most of the warnings enabled and treated as errors across three compilers (MSC, GCC, Clang, in various versions). It's actually depressing how once or twice per year compiler updates point out previously undetected cases.

[1] If I recall correctly, none of the compilers actually have a warning about leaving a variable uninitialized. They just warn about using potentially uninitialized variable, which at least to me seems like a much harder thing to detect.

-JLarja
< Previous Post in ThreadNext Post in Thread >
TopicPosted ByDate
Why not initialize all variables to zero?Doug S2020/03/26 12:13 PM
  Most (all?) modern programming languages do this, right?Mark Roulo2020/03/26 04:44 PM
    Most (all?) modern programming languages do this, right?Konrad Scharz2020/03/27 01:09 AM
      Most (all?) modern programming languages do this, right?Gionatan Danti2020/03/27 03:13 AM
        Most (all?) modern programming languages do this, right?Foo_2020/03/27 03:45 AM
          Most (all?) modern programming languages do this, right?Gionatan Danti2020/03/27 09:19 AM
            Most (all?) modern programming languages do this, right?Foo_2020/03/28 02:32 AM
              Most (all?) modern programming languages do this, right?Montaray Jack2020/03/28 10:26 AM
      Most (all?) modern programming languages do this, right?Jeff S.2020/03/27 07:22 AM
        Most (all?) modern programming languages do this, right?anonymou52020/03/27 01:08 PM
  Why not initialize all variables to zero?Etienne2020/03/27 01:56 AM
    Why not initialize all variables to zero?NoSpammer2020/03/27 02:31 AM
  Why not initialize all variables to zero?Carlie Coats2020/03/27 06:17 AM
    Why not initialize all variables to zero?Jukka Larja2020/03/27 10:14 PM
      Why not initialize all variables to zero?Anon2020/03/28 12:01 AM
        Why not initialize all variables to zero?Jukka Larja2020/03/28 08:25 AM
          Why not initialize all variables to zero?Anon2020/03/28 11:20 AM
            Why not initialize all variables to zero?Jukka Larja2020/03/28 11:45 AM
              Why not initialize all variables to zero?Anon2020/03/28 01:21 PM
                Why not initialize all variables to zero?Jukka Larja2020/03/28 09:49 PM
    Why not initialize all variables to zero?Doug S2020/03/28 11:27 AM
      Why not initialize all variables to zero?Anon2020/03/28 01:24 PM
        Why not initialize all variables to zero?Carlie Coats2020/03/29 06:56 AM
  Why not initialize all variables to zero?Gabriele Svelto2020/03/27 06:52 AM
    Why not initialize all variables to zero?Foo_2020/03/28 02:34 AM
      Why not initialize all variables to zero?Linus Torvalds2020/03/28 09:40 AM
        Why not initialize all variables to zero?Doug S2020/03/28 11:21 AM
          Why not initialize all variables to zero?Linus Torvalds2020/03/28 01:01 PM
            Why not initialize all variables to zero?Etienne2020/04/02 01:14 AM
              Why not initialize all variables to zero?gallier22020/04/02 05:41 AM
              Why not initialize all variables to zero?Doug S2020/04/02 09:51 AM
        Why not initialize all variables to zero?Gabriele Svelto2020/03/28 01:46 PM
          Why not initialize all variables to zero?Linus Torvalds2020/03/28 04:28 PM
            Why not initialize all variables to zero?Anon32020/03/29 04:23 AM
            Why not initialize all variables to zero?Gabriele Svelto2020/03/29 12:28 PM
              Why not initialize all variables to zero?Anon32020/03/29 01:05 PM
                Why not initialize all variables to zero?Gabriele Svelto2020/03/30 12:52 AM
        Why not initialize all variables to zero?Carlie Coats2020/03/29 07:03 AM
        Why not initialize all variables to zero?gallier22020/03/29 11:48 PM
          Why not initialize all variables to zero?Michael S2020/03/30 02:24 AM
            Why not initialize all variables to zero?gallier22020/03/30 03:11 AM
        Why not discard variables after last use?2020/03/31 08:02 AM
          Makes no sense at allHeikki Kultala2020/03/31 01:01 PM
            An example (maybe)Mark Roulo2020/03/31 04:07 PM
              An example (maybe)Doug S2020/04/01 11:01 AM
                An example (maybe)Simon Farnsworth2020/04/02 02:21 AM
            Why not discard variables after last use?2020/04/02 12:41 PM
    Why not initialize all variables to zero?j2020/03/28 09:16 AM
    Why not initialize all variables to zero?Montaray Jack2020/03/28 11:42 AM
  Why not initialize all variables to zero?blaine2020/03/27 01:23 PM
    Why not initialize all variables to zero?James2020/03/28 03:18 AM
      Why not initialize all variables to zero?Anon32020/03/28 05:14 AM
      Why not initialize all variables to zero?Doug S2020/03/28 11:32 AM
        Why not initialize all variables to zero?Anon32020/03/28 11:45 AM
    Why not initialize all variables to zero?gallier22020/03/30 12:03 AM
  Why not initialize all variables to zero?gallier22020/03/29 11:32 PM
    Why not initialize all variables to zero?Michael S2020/03/30 02:30 AM
      Why not initialize all variables to zero?gallier22020/03/30 03:45 AM
Reply to this Topic
Name:
Email:
Topic:
Body: No Text
How do you spell purple?