4

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]?

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • 1
    Is there any way at all to catch this? – DanielGibbs Nov 18 '12 at 09:22
  • 3
    More exactly, call stack overflow. – John Dvorak Nov 18 '12 at 09:22
  • To deal with it, do not allocate large arrays on the call stack => use `malloc` or `new` – John Dvorak Nov 18 '12 at 09:23
  • @DanielGibbs: No, there is no way to catch this.It is not a standard C++ exception. It is an OS exception. – Alok Save Nov 18 '12 at 09:23
  • possible duplicate of [automatic/static memory allocation](http://stackoverflow.com/questions/4379394/automatic-static-memory-allocation) – raina77ow Nov 18 '12 at 09:27
  • Fair enough. Although in cases of extremely low memory systems, does that mean that almost anything should be dynamically allocted? – DanielGibbs Nov 18 '12 at 09:28
  • 1
    @DanielGibbs If you mean embedded systems, then the stack space could be shared with the heap space. It this case, static allocation saves a few bytes. On the other hand, dynamic allocation failures can be handled more gracefully. – John Dvorak Nov 18 '12 at 09:31
  • 2
    If it's global, program just can't start. If it's local, it causes stack overflow. You can't handle these errors. – frp Nov 18 '12 at 10:47
  • @DanielGibbs Some systems make it possible to catch errors resulting from stack overflow, it's just not *portably* possible. On Linux you could install a handler for `SIGSEGV` and compare the fault location to a calculated stack limit. – user4815162342 Nov 18 '12 at 11:27

1 Answers1

5

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.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • In some scenarios (Without VLA or recursion) it should be possible to determine the required stack size statically; do tools do that? – Kos Nov 18 '12 at 10:47
  • Normal compilers and IDEs don't do that, partly because they can't know what the target stack size (also configurable) will be. – user4815162342 Nov 18 '12 at 10:52