6

In C there are 2 ways to create arrays:

int array[100]; 

and

int * array = malloc(sizeof(int)*100); 

With the second statement its easy to check if there was enough memory available to create the array for example:

if(array == NULL){
   goto OutOfMemory;
}

But how would you check that the first worked successfully? Assuming this was running on a microcontroller and not a computer.

Dean
  • 8,668
  • 17
  • 57
  • 86

2 Answers2

10

There is no such thing as a recoverable failure from allocation of an array on the stack (the first way). It will only fail if allocating it causes a stack overflow, at which point your program has aborted/terminated anyway.

When you allocate the array the first way, it is being allocated on the stack, usually at function call time. If there isn't enough room on the stack to allocate it, the program aborts with a stack overflow/segfault error.

When you allocate the second way, you are asking the memory manager for memory on the heap at the time you actually call malloc.

EDIT: As mentioned by @Deduplicator, if you're on a system without memory protection, not having enough free stack space to allocate an array can leave to overruns and much subtler problems (although most likely it will fail on an illegal instruction soon enough).

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • 1
    No guarantee for systems without memory protection / MMU for stack overflow. Those are funnier. – Deduplicator Apr 24 '14 at 22:52
  • @Deduplicator: Edited. And if you get really unlucky, you end up overwriting executable memory with (technically) valid instructions. I would not want that happening on a robot control system... – Linuxios Apr 24 '14 at 22:55
  • Why does nobody love SkyNet? – Deduplicator Apr 24 '14 at 22:55
  • 1
    @Deduplicator: Didn't you get the news? SkyNet has been renamed OneNet after a trademark lawsuit. Even killer AI is subject to trademark law :). – Linuxios Apr 24 '14 at 22:57
  • Well, the first method is "recoverable" if you count editing the source code to reduce the stack size required, recompiling, and rerunning the program. – wallyk Apr 24 '14 at 23:01
  • @wallyk: True... I don't think I consider that recoverable to the program though... – Linuxios Apr 24 '14 at 23:02
1

The first piece of code stores the array in the stack The second stores the array in the heap

Stack memory is pre-allocated thorughout the thread, having said that, unless u'r allocating huge amounts of data on the stack, you shouldn't be generally worried about stack space.

Checking available stack size in C

Edit: In that case you ought to make sure, in-advance that you have enough stack (defined in your IDE/Compiler/Linker/Proprietary software) for the depthest of the calls throughout your code execution. This can be known in advance in compile-time, no need for runtime checks.

Community
  • 1
  • 1
HLL
  • 169
  • 10
  • Further info: `stack` and `heap` are programming jargon. In C the terms are `automatic storage` and `free store`. The free store is not required to use a heap data structure, and the automatic storage is not required to use a stack data structure. – M.M Apr 25 '14 at 02:43