
1. What are Uninitialized Reads
Erroneous behaviour for uninitialized reads, or uninitialized reads. In traditional C++, UB (Undefined Behavior) is a very broad term; any behavior that does not fall under normal operations can be considered undefined. This overly broad definition often leads to a problem where, after an issue arises, it is difficult to pinpoint the cause. The handling of UB is simple, but it does not align with the principles of controllable safety.Erroneous behaviour for uninitialized reads can be seen as a specific case of this UB behavior. From this perspective, this development direction aligns with the overall direction of all programming languages, including C++. Therefore, in C++26, uninitialized reads are defined as errors, allowing for a clearer distinction of the final determinism of behavior.
2. Uninitialized Values
In previous C++ development, there has been a repeated emphasis on initializing variables. Especially for stack variables, if they are not explicitly initialized, UB behavior is likely to occur during operations. UB behavior can manifest as normal execution, unexpected execution, or even cause program crashes… After all, for strongly typed languages like C++, the overall process of program execution is hoped to be a clearly defined, safe, and exception-controllable process. However, in reality, developers often have varying levels of skill, and the code they write can differ significantly. Unfortunately, this results in the ideal of having a clear and safe process being overly optimistic. Therefore, clearly defining erroneous or exceptional behavior is a very necessary situation. See the example below:
void test(){
int data;
std::cout<<"data value is:"<<data<<std::endl;
}
Most programmers understand that in the above example, the value of data is indeterminate. It can also be referred to as EB (Erroneous Behavior). The new C++ standard aims to make errors and exceptions more determinable, reducing ambiguity. C++26 aligns with this trend. Of course, there are many types of initialization, including zero initialization, forced initialization, default initialization, and value initialization, which are all defined accordingly. These will not be elaborated on here; once the standard is finally released, everyone can refer to the relevant content directly.To achieve safe operational behavior, the new standard also requires an updated definition for uninitialized variables, namely, “Default-initialization of an automatic-storage object initializes the object with a fixed value defined by the implementation; however, reading that value is a conceptual error.” In other words, reading certain default-initialized values is also categorized as an error, and the standard is expected to provide mechanisms to detect such issues. Of course, to maintain backward compatibility, it may also allow ignoring such issues and consider them as valid behavior or provide mechanisms to exit and restore such behavior.It should be noted that this definition is a cautious exploration, effective only for single variable behavior, and does not support the use of multiple values, etc. More specific details will await the final release of the standard. For example, see the following:
void f() {
char data[] = {'s', 'e', 'c', 'r', 'e', 't'}; // automatic variable
::new (static_cast<void*>(data)) char; // default-initialization
}
3. Exit Mechanism
As mentioned above, there are situations where initialization may not be necessary, and two mechanisms can be employed: one is to explicitly use a specifier, such as noinit; the other is to use an attribute mechanism, as shown in the code below:
int x = noinit; // First method
int x [[indeterminate]]; // Second method
Of course, as always, the final situation will depend on the final state of the standard.
4. Conclusion
The introduction of the new standard aims to further enhance the overall safety and controllability of the C++ language. Humans have a natural fear of the unknown and uncontrollable, and indeed, uncertainty often leads to unknown problems. For programming, which is a tool to assist humans, uncertainty is unwelcome. However, it must be said that progress must be made step by step, and tasks must be handled one at a time.