2. Stack vs. Heap
-
Stack size is usually limited, heap is not:
$ ulimit -a | egrep 'stack|data' data seg size (kbytes, -d) unlimited stack size (kbytes, -s) 8192 $
main program can allocate on stack and pass to functions,
but functions can not allocate on stack and pass back to main:char *f( int n) { char s[n]; // allocated on stack return s; // won't work, space for s goes away when function returns } char *g( int n) { char *s = malloc(n); // allocated on heap return s; // ok }