First let discover how to use it;
void * alloca(size_t size)
So its usage is like malloc, except that it allocates in a stack portion of memory, not the heap which means, allocating a block with alloca is an explicit action; you can allocate as many blocks as you wish, and compute the size at run time. But all the blocks are freed when you exit the function that alloca was called from, just as if they were automatic variables declared in that function. There is no way to free the space explicitly.
- Using alloca wastes little space than malloc and is very fast
- alloca does not cause memory fragmentation
- Automatically free the space allocated with alloca when they exit through the function that called alloca, no memory leakage.
- If you try to allocate more memory than the machine can provide, you don’t get NULL pointer as a return value, you’ll get a stack overflow.
- It is less portable than malloc, not supported by all compilers.
- Don’t return with its pointer
- Don’t store its pointer to global variable of structure allocated in heap or …
- Don’t use it inside arguments of function call