在栈上动态分配内存
使用函数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都支持。
使用函数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);
本文介绍如何使用alloca函数在栈上动态分配内存。与堆内存分配不同,通过alloca分配的内存会在函数返回时自动释放,无需显式调用free。但需要注意的是,不是所有系统都支持alloca函数,且其使用受到限制。
3059

被折叠的 条评论
为什么被折叠?



