alloca 函数
来源:互联网 发布:淘宝订单评价有效期 编辑:程序博客网 时间:2024/06/05 16:29
The answer is right there in the man
page (at least on Linux):
RETURN VALUE The alloca() function returns a pointer to the beginning of the allocated space. If the allocation causes stack overflow, program behaviour is undefined.
Which isn't to say it should never be used. One of the OSS projects I work on uses it extensively, and as long as you're not abusing it (alloca
'ing huge values), it's fine. Once you go past the "few hundred bytes" mark, it's time to use malloc
and friends, instead. You may still get allocation failures, but at least you'll have some indication of the failure instead of just blowing out the stack.
One of the most memorable bugs I had was to do with an inline function that used alloca
. It manifested itself as a stack overflow (because it allocates on the stack) at random points of the program's execution.
In the header file:
void DoSomething() { wchar_t* pStr = alloca(100); //......}
In the implementation file:
void Process() { for (i = 0; i < 1000000; i++) { DoSomething(); }}
So what happened was the compiler inlined DoSomething
function and all the stack allocations were happening inside Process()
function and thus blowing the stack up. In my defence (and I wasn't the one who found the issue, i had to go and cry to one of the senior developers when i couldn't fix it), it wasn't straight alloca
, it was one of ATL string conversion macros.
So the lesson is - do not use alloca
in functions that you think might be inlined.
- alloca 函数
- 堆,栈,函数,alloca
- 笔记–alloca函数
- 关于alloca()函数
- alloca
- 内存分配函数——alloca()
- 《c专家编程》笔记--alloca函数
- calloc(), malloc(), realloc(), alloca()函数的区别
- 栈的动态分配 ALLOCA函数使用
- malloc,calloc,alloca和free函数
- malloc、calloc、realloc、alloca几个内存分配函数的区别
- 动态内存申请函数选择(realloc、malloc 、alloca、 calloc)
- 动态内存申请函数选择(realloc、malloc 、alloca、 calloc)
- c语言内存函数malloc、calloc、realloc、alloca比较
- malloc、calloc、realloc、new以及alloca函数区别
- c语言内存函数malloc、calloc、realloc、alloca比较
- alloca.c
- alloca——可以在栈中动态分配内存的函数
- Davinci DM6446开发攻略——DSP开发工程建立
- 机器学习中的算法:决策树模型组合之随机森林(Random Forest)
- C++Primer学习札记_const限定符详解
- poj3624 Charm Bracelet DP 01背包问题
- 【精确覆盖问题】DLX算法
- alloca 函数
- 简谈Filter
- SQL server数据的存储过程
- Java基础视频教程
- Android学习笔记(十六)——碎片之间进行交互(附源码)
- 一个好用的MATLAB工具包VLFeat
- poj3628 Bookshelf 2 DP 01背包
- JAVA 网络长短连接
- 开始python学习