Please tell me if memory is allocated for variables if they are just declared, but they are not assigned specific values. For example, I simply wrote int x, y, z and at the same time did not use them in any way during the program.
3 answers
If you have not used it at all, the smart optimizer will throw them away without allocating memory. If you used x to read without prior recording, and before that you simply wrote int x; not in the global scope - space will be allocated, but not initialized, which the smart compiler should warn about - the use of an uninitialized variable.
A piece of the book "C. Handbook. Full language description":
An object declaration is a definition if it allocates memory for an object. Declarations that include initializers are always definitions. In addition, all declarations in a function block are definitions, unless they contain the extern. memory class specifier extern. Here are some examples:
int a = 10; // Определение a. extern double b[]; // Объявление массива b, определенного // в другом месте программы. void func() { extern char c; // Объявление, но не определение c. static short d; // Определение d. float e; // Определение e. /* ... */ } If you declare an object outside of all functions, without an initializer and without an extern memory specifier, such a declaration is a tentative definition. Here are some examples:
int i, v[]; // Предполагаемые определения i, v и j. static int j; The intended definition of the identifier remains a simple declaration if the translation unit contains another definition of the same identifier. If not, then the compiler behaves as if the intended definition includes an initializer with a null value, which makes it a definition. Thus, the variables i and j type int in the previous example, whose identifiers are declared without initializers, are implicitly initialized to 0, and the array v with the element type int has one element with the original value 0.
- It is worth adding that if there are definitions of external variables with the same name in different compilation units (ie, we explicitly initialize in several units), then the linker will swear a
multiple definition, and if there is no initialization anywhere (or only in one file), then all OK. Moreover, the types of such variables in different files may be different (however, if the alignment rules do not match, the linker will issue a warning) - avp
You implicitly assume that the compiler honestly allocates memory for each variable separately. This is no longer the case.
Modern optimizers can put any variable into registers, into a common memory area with another variable, if their lifetime does not overlap, or even convert the code and eliminate the variable. Therefore, it makes no sense to save on variables, the compiler will do everything correctly and economically.
Example : this is the code
int f() { int x = 5, y = 8; x += y; y = 7 * x; return x + y; } intel compiler 17 compiles to
push 104 pop rax ret and gcc 6.3 in
mov eax, 104 ret You see that the compiler threw out all the variables and calculations at all.
For example, such a code terminates abnormally (MinGW)
int main() { double v[1000000]; return 0; } In this case, apparently stands out. In the course of the stack overflow.
- yes, of course ideone.com/okYEJX - KoVadim
- Depends on the specific compiler / optimizer ... - Harry
- Compile with the -O key - avp