Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is stack memory allocated when it is not used?

Consider the following example:

struct vector {
    int  size() const;
    bool empty() const;
};

bool vector::empty() const
{
    return size() == 0;
}

The generated assembly code for vector::empty (by clang, with optimizations):

push    rax
call    vector::size() const
test    eax, eax
sete    al
pop     rcx
ret

Why does it allocate stack space? It is not used at all. The push and pop could be omitted. Optimized builds of MSVC and gcc also use stack space for this function (see on godbolt), so there must be a reason.

like image 951
Dr. Gut Avatar asked Jan 07 '20 22:01

Dr. Gut


People also ask

Why stack is used for memory allocation?

Stack memory is a memory usage mechanism that allows the system memory to be used as temporary data storage that behaves as a first-in-last-out buffer.

Is stack used for memory allocation?

Stack and a Heap ? Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled.

Is stack memory allocated at run time?

Stack memory is allocated at runtime. Keep in mind that it has to be allocated at runtime, as there is no way for the compiler to know how many times a function is called, or how many times a while loop is executed, or whatever.

Is stack memory automatically freed?

Advantages and disadvantages Another feature is that memory on the stack is automatically, and very efficiently, reclaimed when the function exits, which can be convenient for the programmer if the data is no longer required.


1 Answers

It allocates stack space, so the stack is 16-byte aligned. It is needed, because the return address takes 8 bytes, so an additional 8-byte space is needed to keep the stack 16-byte aligned.

The alignment of stack frames can be configured with command line arguments for some compilers.

  • MSVC: The documentation says that the stack is always 16-byte aligned. No command line argument can change this. The godbolt example shows that 40 bytes are subtracted from rsp at the beginning of the function, which means that something else also affects this.
  • clang: The -mstack-alignment option specifies the stack alignment. It seems, that the default is 16, although not documented. If you set it to 8, the stack allocation (push and pop) disappears from the generated assembly code.
  • gcc: The -mpreferred-stack-boundary option specifies the stack alignment. If the given value is N, it means 2^N bytes of alignment. The default value is 4, which means 16 bytes. If you set it to 3 (i.e. 8 bytes), the stack allocation (sub and add for rsp) disappears from the generated assembly code.

Check out on godbolt.

like image 108
geza Avatar answered Nov 02 '22 10:11

geza