Stack memory allocation of variables in c++

2.7k Views Asked by At

How does the c++ compiler organize the variables that are initialized in a function to store them so the computer will find them the fastest way?

I understand that the compiler puts them one after an other on the stack but there has to be some logic behind it, I was searching Google for hours on end but I could not find anything.

For example:

int main()
{
    float a;
    int b;
    char c;
    double d;
}

This should occupy more memory than the one below because of the way the c++ compiler is storing in the memory.

The exact bits used are the same, of course, but they should be stored in a more efficient order in the example below. Where in memory would these variables be stored by the compiler in the next example? As far as I understood a variable is always stored on a block such that (logical number) % (number of bytes the datatype) = 0

int main()
{
    char c;
    int b;
    float a;
    double d;
}
3

There are 3 best solutions below

1
On BEST ANSWER

This should occupy more memory than the one below because of the way the c++ compiler is storing in the memory.

Not really, the stack memory consumed by both functions should be the same for any sane optimizing compiler... Modern C++ Compilers are really aggressive at certain optimizations.

Other than suitable alignments, C++ does not impose memory address ordering for automatic variables in functions. While that is true, the observable behavior of the program must not be changed by the compiler.

I guess you are trying to talk about structs and classes where the memory layout and address ordering of variables are as declared.

How does the c++ compiler organize the variables that are initialized in a function to store them so the computer will find them the fastest way?

In practice, every access to an automatic variable in C++ is a simple pointer offset with respect to the stack pointer1 (except variables the compiler placed directly in a register). Additionally, to speed things up to such automatic variables (in no order):

  • The Compiler eliminates dead variables

  • The compiler will find the best order to store each of them to meet suitable alignment

  • The compiler may use the CPU register directly depending on what it's Register Allocation algorithm decides

  • The compiler may lump certain variables together into a vector register and use vector instructions provided it will yield correct results.

  • ...and so much more.


1: Even the stack pointer is aligned by most compilers.

0
On

There is no requirement in the C++ standard for automatic (what you called stack) variables to be laid out in a certain order or location (as long as alignment requirements are satisfied).

0
On

Registers and optimizations.
The most efficient access of variables is to eliminate them. Many compilers will optimize away variables that are not used.

If a variable is used locally, the compiler may decide to place the variable in a register. Registers are the most efficient access for variables.

A stack is a convenient data structure for allocating local variables. The compiler can destroy variables on the stack by changing the stack pointer. Implementations that use a stack often have a pointer to the top of the stack (where the next variable is allocated). Allocation is as simple as adjusting the pointer by a constant (which is an arithmetic operation).

Remember, there is no requirement that a compiler use a stack.