Why is zero-initialization not the default for non-initialized variables in c++? Is there a compiler option to force it?

One of the founding principals of c++ is to not force developers to pay for what they don't use. If you write something like int x; x = 1; then you shouldn't have to pay for the zero initialization of x, even if that cost happens to be very tiny.

Edit : Regarding your other two points

is it preferable to have undefined behavior?

Undefined behavior is not necessarily a bad thing to have in the language (you can argue both ways). It's definitely a bad thing if you write code that causes it. Notably it gives more freedom to implementers and enables important optimizations.

if I want to avoid undefined behavior, I have to initialize it anyway, so what did I win?

It's not undefined behavior to have an uninitialized variable. It's undefined behavior to try to read from one.


The down-side: Potential errors because of uninitialized values.

The up-sides:

  1. Efficiency. You don't pay for it if you don't need it.
  2. The compiler doesn't add assumptions about the right init value. Per @chris in the comments to the OP, you can get a false sense of security since 0 is not always the right initialization value. If it should start as -1, then 0 is wrong.

The mitigations:

  1. Use constructors to initialize your non-POD types
  2. Use "Almost Always Auto" to enforce initialization at declaration time. (Or as @Peter notes in the comments, use explicit types but don't declare a variable until you can properly initialize it. This is implied by AAA, but AAA makes it harder to get wrong since you can't accidentally forget to initialize because the initialization carries the type information also.)
  3. Crank up compiler warnings. For gcc, use -Wall -Werror, which includes -Wuninitialized and then will error on uninitialized values.

The origin: In the hoary old days of C, all declarations came first before initialization. Compare this example from the K&R book:

int main(void)
{
  double sum, atof(char s[]);
  char line[MAXLINE];
  int getline(char line[], int max);

  sum = 0;
  while (getline(line, MAXLINE) > 0)
    printf("\t%g\n", sum += atof(line));
  return 0;
}

C++ retained compatibility with C in this regard, but that came with what is an unfortunate default behavior.

Tags:

C++