V3070. Uninitialized variables are used when initializing the 'A' variable.

The analyzer detected a possible error that has to do with initializing a class member to a value different from the one the programmer expected.

Consider the following example:

class AClass {
  static int A = B + 1;
  static int B = 10;
}

In this code, the 'A' field will be initialized to the value '1', not '11', as the programmer may have expected. The reason is that the 'B' field will be referring to '0' when the 'A' field will be initialized. It has to do with the fact that all the members of a type (class or structure) are initialized to default values at first ('0' for numeric types, 'false' for the Boolean type, and 'null' for reference types). And only then will they be initialized to the values defined by the programmer. To solve this issue, we need to change the order in which the fields are processed:

class AClass {
  static int B = 10;
  static int A = B + 1;
}

This way, the 'B' field will be referring to the value '10' when the 'A' field will be initialized, as intended.

This diagnostic is classified as:

You can look at examples of errors detected by the V3070 diagnostic.


Bugs Found

Checked Projects
411
Collected Errors
14 123
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →
Accept