Uninitialized member variables are are initialized when creating new object in loop
If the memory is uninitialized, everything can be in it!
It seems the compiler reuses the "old" memory addresses of old temporary variables.
Initialize all members like that in ctor:
Student::Student(string studentInformation)
:firstname(), lastname(), id(0), gpa(0)
{
... ctor stuff
}
Uninitialized variables and members in Java
The rules for definite assignment are quite difficult (read chapter 16 of JLS 3rd Ed). It's not practical to enforce definite assignment on fields. As it stands, it's even possible to observe final fields before they are initialised.
Why do I not get compiler warning about access uninitialized member variable in ctor?
Short Answer
As pointed out in the comments, reading from an uninitialized variable is undefined behavior. Compilers are not obligated by the standard to provide a warning for this.
(In fact, as soon as your program expresses undefined behavior, the compiler is effectively released from any and all obligations...)
From section [defns.undefined] of the standard (emphasis added):
undefined behavior
behavior for which this International Standard imposes no requirements
[ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. —end note ]
Long Answer
This can be a difficult situation for a compiler to detect (and if it does detect it, it's difficult to inform the user about it in some useful way).
Your code only exhibits undefined behavior because it's trying to read from uninitialized member variables width
and height
. The fact that they're member variables is one of the things that can make this situation tricky to diagnose.
With local variables, the static analysis involved in detecting this can be relatively straightforward (not all the time, mind you).
For example, it's very easy to see the problem here:
int foo()
{
int a;
int b = 0;
return a + b; // Danger! `a` hasn't been initialized!
}
How about in this scenario:
int foo(int& a)
{
int b = 1;
return a + b; // Hmm... I sure hope whoever gave me `a` remembered to initialize it first
}
void bar()
{
int value;
int result = foo(value); // Hmm... not sure if it matters that value hasn't been initialized yet
}
As soon as we start dealing with variables whose scope extends beyond a single block, it's very difficult to detect whether or not a variable has been initialized.
Now, relating this back to the problem at hand (your question): the variables width
and height
are not local to the constructor - they could have been initialized outside the constructor.
For example:
Image::Image(int _width, int _height)
{
Initialize();
array = new int[width * height]; // Maybe these were initialized in `Initialize`...
width = _width;
height = _height;
}
Image::Initialize()
{
width = 0;
height = 0;
}
Should the compiler emit a warning in this scenario?
After some cursory analysis we can conclusively say "no, it shouldn't warn", because we can see that the Initialize
method does indeed initialize the member variables in question.
But what if Initialize
delegates this to another method MoreInitialize()
? And that method delegates it to another method YetEvenMoreInitialize
? This begins to look like a problem that we can't reasonably expect the compiler to solve.
C++ enable warning for uninitialized variables in classes
I check all warnings with clang 8 (last release version) command line:
clang -O2 -Wall -Wextra -Weverything
Check: https://godbolt.org/z/kKp-N5
Clang hasn't any warnings for uninitialized variables into classes and structs. But using clang-tidy with check cppcoreguidelines-pro-type-member-init may be helpful to you.
https://releases.llvm.org/8.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/cppcoreguidelines-pro-type-member-init.html
Finding uninitialized variables before running script
There's a two options I can currently think of.
- PyLint
This tool can analyse all your source code and try to detect places where bugs may be happening. This should detect things like the one you have mentioned.
- PyCharm
This is an IDE by Jetbrains, and you can turn on spell check for all your variables. While this does not stop typos from happening, it does make it far easier to catch them.
If you're a student, you can get the full version of PyCharm for free.
Related Topics
Matlab VS C++ Double Precision
How to Make Objective-C Class Using Std:Vector Made Available to Swift Classes
How to Output the Value of an Enum Class in C++11
What Is the Fastest Way to Change a Key of an Element Inside Std::Map
The Implementation of Std::Forward
Multiple Definition Error Including C++ Header File with Inline Code from Multiple Sources
Non-Defaulted Operator <=> Doesn't Generate == and != in C++20
Error with Address of Parenthesized Member Function
How to Rearrange a String Equation
Advantages of Std::For_Each Over for Loop
Are C++11 Thread_Local Variables Automatically Static
Netbeans 7.2 Shows "Unable to Resolve Identifier" , Although Build Is Successful
How to Generate Thread-Safe Uniform Random Numbers