Question

When you dynamically allocate memory e.g. malloc(1024 * sizeof(char)) the resulting pointer is set to NULL if there is not enough memory available to fulfill the request.

What happens when there is not enough memory to satisfy a static allocation such as char c[1024]?

Was it helpful?

Solution

char c[1024] is not necessarily a static allocation, it's static or automatic depending if the declaration is written inside the body of a function (without the static modifier) or at top-level.

A static allocation cannot fail at run-time because the space for the allocation is reserved as the program is being executed. If sufficient memory cannot be reserved, the program will fail to load (exec* will fail on Unix). In pathological cases the memory could be overcommitted by the OS, and the system will only reserve it once it is accessed. In that case, failure to allocate will result in the process getting immediately killed by the system.

Automatic allocation simply moves the edge of the stack downward, typically by decrementing the stack pointer register. (This is why allocation of local variables is so fast.) A C program has no portable mechanism to detect that the stack has grown too large. Some operating systems will automatically grow the stack once the MMU detects that you have passed the allocated limit; Linux does this for the main thread, but not with other threads in a process. Even so, sufficient stack allocation will sooner or later surpass a system limit or will exhaust the memory of the system and the program will fail.

Depending on the system, the program will either immediately fail with a segmentation fault or it will die from memory corruption which happens when the stack and the heap start meet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top