【发布时间】:2010-11-17 09:35:36
【问题描述】:
我正在使用一种没有本机堆栈类型的晦涩语言,所以我实现了自己的。现在,在网上阅读我发现了一些不同的方法来做到这一点。
这是我的实现(伪代码)
//push method
function Push(int)
{
Increase (realloc) stack by 4 bytes;
Push int into the new memory area;
}
//pop method
function Pop()
{
collect int from the top of the stack;
reallocate (realloc) the stack to shrink it by 4 bytes;
return int;
}
现在有人说在弹出一个值后使用 realloc() 调用来调整堆栈的大小不利于性能,所以我有几个问题:
- 最好只使用 malloc 增加堆栈然后在程序结束时释放它吗?
- 要调整堆栈大小(推送),最好增加 4 个字节或更多?
- 最佳做法是通过将分配的内存加倍来增加堆栈吗?
- 您对上述代码有何看法?
【问题讨论】:
标签: performance memory-management stack realloc