_alloca

王朝百科·作者佚名  2011-11-01  
宽屏版  字体: |||超大  

函数简介内存分配函数,与malloc,calloc,realloc类似.

但是注意一个重要的区别,_alloca是在栈(stack)上申请空间,用完马上就释放.

包含在头文件malloc.h中.

在某些系统中会宏定义成alloca使用.

#define alloca _alloca

函数原型void * __cdecl _alloca(size_t);

函数说明函数返回一个指向申请到的空间的void型指针.

MDSN中解释如下:

The_allocaroutine returns avoidpointer to the allocated space, which is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other thanchar, use a type cast on the return value. A stack overflow exception is generated if the space cannot be allocated.

注意事项在调用_alloca的函数返回的时候, 它分配的内存会自动释放。

也就是说, 用 alloca 分配的内存在栈上。

_alloca不具可移植性, 而且在没有传统堆栈的机器上很难实现。

当它的返回值直接传入另一个函数时会带来问题,因为他分配在栈上.

由于这些原因,_alloca不宜使用在必须广泛移植的程序中, 不管它可能多么有用。

既然 C99 支持变长数组(VLA), 它可以用来更好的 完成 alloca() 以前的任务。

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝百科 版权所有