Why does this not give a compilation error?

int a = a ; 

According to the rule of the assignment operator, it must read the line from right to left. After viewing the "raw" compiler, a compilation error should occur.
But it is giving garbage value . Please check it out.

+6
source share
2 answers

Actually, the compiler can give you a hint. My says: "Warning C4700: local variable" a "is used without initialization."

But this is not an error in itself, the declaration simply gives the variable name to some bits of memory without touching it, which is really fast. And the adjustment here is not really a snap, just moving the bits from right to left. No checks again. Very productive, very dangerous.

Each operation is legal, but all this is meaningless. Thus, the compiler does everything possible - it compiles the code, but also gives a warning.

+2
source

ยง3.3.2 / 1:

The declaration point for the name immediately after its full declaration (section 8) and before its initializer (if any), except as noted below. [Example:

 int x = 12; { int x = x; } 

Here the second x is initialized with its own (undefined) value. -end example]

+9
source

Source: https://habr.com/ru/post/949282/


All Articles