What will happen as a result of such instructions:

int x = x; 

?

  • four
    And what observable behavior do you expect from this instruction? :) - D-side
  • @ D-side I do not expect the observed. I want to know that, in principle, will be fulfilled - valuev
  • @valuev, what's stopping yourself from trying different options and seeing what happens? - insolor
  • @insolor not understand assembly language at the proper level - valuev
  • 2
    @insolor then what are the "different options" you propose to consider? - valuev

3 answers 3

If the variable x already defined in this scope, it will not compile:

 int x = 0; int x = x; // <-- ошибка, повторная декларация переменной 

http://ideone.com/3VGhe0

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.

  • But what is the formal error? the declaration occurred before the equal sign, after which meaningless assignment is made. Or I do not understand what exactly is happening? - pavel
  • @pavel: Do you mean the first or second case? In the first, overriding a variable that is prohibited by the rules. In the second, the use of an uninitialized value, which is UB by default. - VladD
  • one
    @pavel: In the C ++ string, int x = initializer; the variable x exists and is available after the = sign, not after ; . Therefore, int x = x; makes a link to itself. - VladD
  • Compilers issue warnings, at least on similar code inside functions. I already collected a collection of warnings from GCC, clang and MSVC, but ultimately decided that it did not pull the answer. I would advise to include all warnings and treat them as errors. Mmmmmm maximum severity. - D-side
  • @ D-side: gcc did not seem to give out: ideone.com/cvtZso - VladD

It 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.

  • Some non-response. It would be worth saying what happens in each of these cases. Well and still, to declare a variable in dynamic memory will not work. - Qwertiy
  • What is the issue. =) Specify where this line is located. Inside a function or in a global variable. - hitcode

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); 
  • one
    Is not a fact. Assigning an uninitialized variable is UB, but a declaration without initialization is not. - Qwertiy
  • @Qwertiy You hurried with a comment, I already corrected the answer by finding this place from Sutter. - Harry
  • I can not find. Can you exactly name the book and page number? - Qwertiy
  • @Qwertiy Coat of arms of Sutter. Solving complex problems in C ++. C ++ In-Depth Series. - M .: Publishing house "Williams", 2002. Pp. 339. - Harry
  • I just found the year 2008. There 9.1 on page 338 about if (this != &other ) . Keep looking 2002? - Qwertiy