在栈上动态分配内存

在栈上动态分配内存

使用函数alloca可以实现在栈上动态分配内存:

The function alloca has the same calling sequence as malloc; however, instead of allocating memory from the heap, the memory is allocated from the stack frame of the current function. The advantage is that we don't have to free the space; it goes away automatically when the function returns. The alloca function increases the size of the stack frame. The disadvantage is that some systems can't support alloca, if it's impossible to increase the size of the stack frame after the function has been called. Nevertheless, many software packages use it, and implementations exist for a wide variety of systems.


FreeBSD, Linux, Mac OS X,Solaris都支持。


       #include <alloca.h>

       void *alloca(size_t size);


你可能感兴趣的:(内存)