Search code examples
c++coverity

Does C++ initialize integers to zero automatically?


I've noticed several Coverity (static-analysis tool) errors of type 'Uninitialized scalar variable' that are high impact. A lot of them are just ints that don't get initialized.

Would initializing them to zero be any different than what C++ does by default?


Solution

  • Does C++ initialize integers to zero automatically?

    For automatic variables:

    Some compilers might do it but the standard does not require it. A conforming implementation could leave them to be uninitialized garbage values.

    For static variables:

    They must be initialized to zero unless explicitly initialized otherwise.