Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C standard have any guarantees on the amount of stack space used?

Tags:

c

stack

embedded

I am doing embedded programming where saving memory is important.

How much stack space would the following C code occupy at run-time?

if (send_small_message) {
    uint8_t buffer[16000];
    // do something with the buffer
} else {
    uint8_t buffer[32000];
    // do something the with buffer
}

Could some compiler decide to allocate 16000 + 32000 = 48kB stack space for both buffers? Or is it guaranteed that since both buffers will never be used at the same time, the compiler will allocate only 32kB - the size of the larger buffer?

FOLLOW UP QUESTION:

void SendSmallMessage() {
    uint8_t buffer[16000];
    // do something with the buffer
}

void SendLargeMessage() {
    uint8_t buffer[32000];
    // do something with the buffer
}

Can a code compiled by some compiler use 16000 + 32000 bytes at run-time to execute the snippet below:

if (send_small_message) {
   SendSmallMessage(); 
} else {
   SendLargeMessage();
}
like image 468
mercury0114 Avatar asked Sep 24 '20 13:09

mercury0114


People also ask

What is max size of stack in C?

In Visual Studio the default stack size is 1 MB i think, so with a recursion depth of 10,000 each stack frame can be at most ~100 bytes which should be sufficient for a DFS algorithm. Most compilers including Visual Studio let you specify the stack size.

How much space does the stack have?

On Windows, the typical maximum size for a stack is 1MB, whereas it is 8MB on a typical modern Linux, although those values are adjustable in various ways.

How does functions affect the stack consumption?

Each called function may increase the amount of data on the stack. Those functions that are called may call other functions, and so on. This again, depends on the snapshot in time of the execution. However, one can produce an approximate maximum increase of the stack by the other called functions.

Can the stack run out of space?

stack limits Running out of stack memory is called stack overflow . Most languages have a terrible stack overflow story. For example, in C, if you use too much stack, your program will exhibit "undefined behavior". If you are lucky, it will crash your program; if are unlucky, it could crash your car.


1 Answers

Does the C standard guarantee amount of stack used?

No guarantees whatsoever exist for this. The C standard does not mention concepts like stacks. You can even write C for low level CPUs that completely lack a stack.

The C standard does however guarantee that uint8_t is 1 byte large and that 1 byte is 8 bits on your system (or otherwise uint8_t wouldn't be available).

How much stack space would the following C code occupy at run-time?
Could some compiler decide to allocate 16000 + 32000 = 48kB stack space for both buffers?

System-specific, but also depends on exactly how the function is written and what optimizations that take place. Generally though, real world systems allocate room for as much stack as the function requires, given all possible execution paths. So it is quite likely that many compilers would allocate 16k + 32k.

But who cares, since it doesn't make sense to allocate that large amount of memory on the stack in any known system. Not on high-end, PC-like systems and certainly not on memory-restricted embedded systems. You'll get stack overflows all over the place.

The general rule of thumb in embedded is to never allocate any form of buffers on the stack, but always with static storage duration. On PC-like systems, heap allocation is another option.

like image 90
Lundin Avatar answered Oct 17 '22 19:10

Lundin