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.
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.
rspat the beginning of the function, which means that something else also affects this.-mstack-alignmentoption specifies the stack alignment. It seems, that the default is 16, although not documented. If you set it to 8, the stack allocation (pushandpop) disappears from the generated assembly code.-mpreferred-stack-boundaryoption 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 (subandaddforrsp) disappears from the generated assembly code.Check out on godbolt.