stack에 동적으로 메모리 할당: alloca

C프로그래밍을 하다보면 memory leak에 대한 걱정 때문에 메모리를 동적으로 할당(malloc)하는 것이 부담스럽습니다.

만약 동적으로 할당한 메모리가 함수내에서만 사용된다면 함수 끝에서 free할 필요 없이 malloc 대신 alloca로 메모리를 할당해 사용하면 됩니다. alloca를 사용하면 activation record(stack)에 메모리가 할당되거든요.

NAME
       alloca – allocate memory that is automatically freed

SYNOPSIS
       #include <alloca.h>

       void *alloca(size_t size);

DESCRIPTION
       The  alloca() function allocates size bytes of space in the stack frame
       of the caller. This temporary space is automatically  freed  when  the
       function that called alloca() returns to its caller.