What will happen as a result of such instructions:
int x = x; ?
If the variable x already defined in this scope, it will not compile:
int x = 0; int x = x; // <-- ошибка, повторная декларация переменной If there is no variable x in this scope (for example, it is not defined at all, or defined in the outer area), then this will be compiled, but the program will have undefined behavior . This means that you are not even guaranteed that there will be any garbage in the variable. The program has the right to do anything: from the point of view of the compiler, it is meaningless. Never do that: the C ++ compiler considers you quite mature and responsible, and does not check whether you are following the rules.
Yes, I do not know why in this case the compiler does not give an error. Other languages trust the programmer less and control.
int x = initializer; the variable x exists and is available after the = sign, not after ; . Therefore, int x = x; makes a link to itself. - VladDIt depends on where it is, this line.
There are three types of memory, dynamic, static and heap.
1] Dynamic memory: usually allocated new, malloc, alloc..itd memory is allocated and there is garbage (remnants of previous work), but in some cases the memory 0 is filled, and the new constructor can fill the memory
2] static memory (global change, static): Memory is allocated by the compiler and filled with a zero value. It can be seen in the binary and change.
3] stack (int x inside a function): memory is allocated, but there is garbage in it.
In practice, exactly the same thing happens as with a simple declaration.
int x; However, in theory, this is undefined behavior, so whatever ...
This question was mentioned by Sutter in “ Challenges ”, namely in problem 9.1, postscript 1.
There is one more similar code, but with the new operator:
T t; new(&t) T(t); if (this != &other ) . Keep looking 2002? - Qwertiy ♦Source: https://ru.stackoverflow.com/questions/521216/
All Articles