If your variable is a primitive type (int, char, etc.):
For a global or static variable no. This is just an entry in the BSS or DATA segment (depending on whether it was initialized or not), no executable code is required. Except, of course, if the initializer needs to be evaluated at runtime.
For a local variable, if it is not initialized, usually the first implies an assembly instruction, the others do not. This is because the space allocation for them is usually done by adding an offset to the stack pointer (in fact, subtraction - the stack grows backward). When you declare your first int variable, "ADD SP, 4" is generated; for the second, he just changed to "ADD SP, 8". This instruction will not be in the place where you declare your variable, but instead at the beginning of the function, because there should be allocated the whole stack space for local variables.
If you initialize a local variable at creation, then you will have a MOV command to load the value into its location on the stack. This instruction will be in the same place as the declaration regarding the rest of the code.
These rules for local variables do not imply any optimization. One of the common forms of optimization is the use of CPU registers as variables, in this case distribution is not required, but initialization will generate an instruction. In addition, sometimes these registers must retain their values, so you will see the PUSH instruction at the beginning and POP at the end of the function.
The rules for objects when the constructor is not involved (or the constructor is nested) are much more complicated, but the same logic is used. If you do not have a built-in constructor, you will need at least instructions to call it.
source share