4 ms·
Because it wastes memory and decreases performance, that's why. If there's a section of code in your function that may or may not be executed, depending on a c
by Grishnakh 11y ago
Because it wastes memory and decreases performance, that's why.
If there's a section of code in your function that may or may not be executed, depending on a conditional, then allocating the memory for variables used within that code block will be a waste if you end up not executing it. Now, you could argue that that block should be turned into a separate function, but now you're doing another function call (unless it gets optimized out) which wastes performance, and you're making the code more cluttered if the code block is relatively small and not really worth turning into a separate function.
- majewsky 11y ago> allocating the memory for variables Allocation of N variables on the stack is O(1) since it just moves the stack pointer by the total size of all variables.
- tboneatx 11y agoAre not all the local variables allocated on the stack in the function prologue anyway? no matter where in the function they are defined. A typical function starts with subtracting the stackpointer to make space for the local variables if i'm not mistaken.