Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alloca function in C

Tags:

c

alloca

I was revising C and came across to alloca/free functions which is described as allocating storage on a stack like space. Is this same as the malloc/free ? or this is something different ? Thanks.

like image 903
Cemre Mengü Avatar asked Feb 04 '12 21:02

Cemre Mengü


2 Answers

I think you mean alloca which is used to allocate memory on the stack. And yes, this is different from malloc and free which allocate on the heap.

Don't attempt to free memory allocated with alloca because it is automatically freed when the function that calls alloca returns to its caller.

Using alloca and/or C99 variable length arrays can be a risky business because you can readily overflow the stack if you use these tools imprudently.

like image 178
David Heffernan Avatar answered Sep 20 '22 09:09

David Heffernan


malloc, calloc and realloc functions allocate space on the heap.

free function "frees" space previously allocated by these functions.

like image 40
Eregrith Avatar answered Sep 21 '22 09:09

Eregrith