Does C++ initialize integers to zero automatically?

PerryC picture PerryC · Jul 13, 2015 · Viewed 9k times · Source

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?

Answer

R Sahu picture R Sahu · Jul 13, 2015

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.