Whenever you highlight local variables in the C domain (such as functions), they do not have a default initialization code (for example, C ++ constructors). And since they are not dynamically allocated (these are just uninitialized pointers), additional (and potentially expensive) functions should not be called (e.g. malloc ) to prepare / highlight them.
Due to the stack working, highlighting the stack variable simply means decreasing the stack pointer (i.e. increasing the size of the stack because it grows down on most architectures) to make room for it. From the CPU point of view, this means executing a simple SUB instruction: SUB rsp, 4 (in case your variable is 4 bytes in size - for example, a 32-bit integer).
In addition, when you declare several variables, your compiler is smart enough to actually group them together into one big SUB rsp, XX instruction, where XX is the total size of the local variables in the area. In theory. In practice, something else happens.
In such situations, I find GCC explorer an invaluable tool when it comes to figuring out (with great ease) what happens “under the hood” of the compiler.
So, let's see what happens when you actually write a function like this: GCC Explorer Link .
C code
int function(int a, int b) { int x, y, z, t; if(a == 2) { return 15; } x = 1; y = 2; z = 3; t = 4; return x + y + z + t + a + b; }
Resulting assembly
function(int, int): push rbp mov rbp, rsp mov DWORD PTR [rbp-20], edi mov DWORD PTR [rbp-24], esi cmp DWORD PTR [rbp-20], 2 jne .L2 mov eax, 15 jmp .L3 .L2: -- snip -- .L3: pop rbp ret
As it turned out, GCC is even smarter than that. It does not execute the SUB command at all to distribute local variables. It simply (internally) assumes that the space is “occupied”, but does not add any instructions for updating the stack pointer (for example, SUB rsp, XX ). This means that the stack pointer is not updated, but since in this case the PUSH instructions (and no rsp -relative lookups) are rsp after using the stack space, there is no problem.
Here's an example where no additional variables are declared: http://goo.gl/3TV4hE
C code
int function(int a, int b) { if(a == 2) { return 15; } return a + b; }
Resulting assembly
function(int, int): push rbp mov rbp, rsp mov DWORD PTR [rbp-4], edi mov DWORD PTR [rbp-8], esi cmp DWORD PTR [rbp-4], 2 jne .L2 mov eax, 15 jmp .L3 .L2: mov edx, DWORD PTR [rbp-4] mov eax, DWORD PTR [rbp-8] add eax, edx .L3: pop rbp ret
If you look at the code before returning prematurely ( jmp .L3 , which switches to the clear and return code), additional instructions are not needed to "prepare" the stack variables. The only difference is that the parameters of the function a and b, which are stored in the edi and esi registers, are loaded edi stack with a higher address than in the first example ( [rbp-4] and [rbp - 8] ). This is due to the fact that additional space was not "allocated" for local variables, as in the first example. So, as you can see, the only “overhead" for adding these local variables is to change the subtracted term (i.e. not even add the extra subtraction operation).
Thus, in your case, there is practically no cost to simply declaring stack variables.