I also read somewhere else (not sure tho), that class variables (non-static) are also initialized to 0.
Wherever you read that (if you read that), then stop reading that source!
Is this true?
No.
Here's a short example:
#include <iostream>
class Foo {
public:
int* ip; // Do we get default initialization to "nullptr" ?? ...
Foo() = default;
};
int main()
{
Foo Bar;
std::cout << Bar.ip << std::endl; // ... print the "ip" pointer to see!
return 0;
}
The output when building with clang-cl and running on Windows 10 is (but the actual value varies):
00000272B7D941D0
When compiling with MSVC, the following message is given:
warning C4700: uninitialized local variable 'Bar' used
Of course, some compilers may set such uninitialized memory to zero, and even when built with Clang or MSVC, as above, the initial value of the pointer may occasionally just happen to be zero. However, the warning from MSVC should be taken seriously. Furthermore, the static analyser that clang-cl uses gives a more specific warning:
std::cout << Bar.ip << std::endl;
^
warning GDEC5F24A: 1st function call argument is an uninitialized value [clang-analyzer-core.CallAndMessage]
… is there any way I can tell my compiler not to do this
(using some compiler flag)?
Probably not – but you can enable all compiler warnings, which will show you cases where you have such behaviour.