有关alloca()的问题以及在实现中使用堆栈的替代方法 [英] Question about alloca() and alternative to using stack in an implementation

查看:269
本文介绍了有关alloca()的问题以及在实现中使用堆栈的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大师


我正在阅读常见问题


alloca无法编写,并且很难在

没有传统堆栈的机器。


我明白标准并没有强制要求堆。或者堆栈


我很想知道没有堆栈或堆的实现。

他们如何管理本地和全局内存?虽然这可能是一个很少偏离主题但这有助于我理解委员会的上述理由背后的原因。

我希望能得到有趣的答案,因为在我有限的曝光中我已经看到了一堆堆栈n堆的机器和来自全球的人

论坛会让我一些答案关于机器谁没有

堆栈和/或堆然后运行标准C.


提前谢谢

-Sushil Ramachandran

解决方案

一个例子是PIC控制器!

PIC控制器确实有内部硬件堆栈只有几个

(8-32)字的深度。这个硬件堆栈只提交给调用并返回

并且堆栈没有push / pop。

所以我的PIC C编译器必须把我所有的本地'' C''变量在全球

空间!

还有缺乏处理内存资源的操作系统!

''C ''将所有这些留给了''C''实现者!

C代码在小型PIC控制器中看起来或多或少相同

看看

a PC例如。

void ex()

{

int var; //在堆栈中在电脑上//全球公羊在PIC

...

}

// jota


" JOTA" < ab*@hotmail.com>写在

新闻:MT ****************** @ newsb.telia.net:

一个例子是PIC控制器!
PIC控制器确实有一个内部硬件堆栈,只有几个字(8-32)字深。这个硬件堆栈只被提交给call和
返回而且没有push / pop到堆栈。
因此我的PIC C编译器必须将所有本地C变量放入

这有点偏离主题,因为它与C语言无关,而是与C编译器实现的
实现。这将在

comp.arch.embedded中讨论得更好。


< OT>

那不是原因。许多32位RISC CPU上没有硬件堆栈

。按照惯例,只使用其中一个寄存器作为堆栈

指针。然后,不是推送和弹出,而是在堆栈指针上使用加载和存储,并使用

add或sub。注册。

Ofcousre还缺少处理内存资源的操作系统!
''C''将所有这些留给了''C''实现者!
C代码在小型PIC控制器中看起来或多或少相同,例如在PC中查看。
void ex()
{
int var; //在pc上的堆栈中//在PIC中的全局ram



它不是全局的,它是函数的本地。它可能会或可能不会与其他函数的其他局部变量叠加

。这也是Keil

编译器如何为8051工作。但是,你失去了重入性,因此在这种模式下,编译器不符合ISO C标准。 < / OT>


-

- 马克 - >

-




" Sushil" <在******* @ yahoo.com>在消息中写道

我很想知道没有堆栈或堆积的实现。
他们如何管理本地和全局内存?虽然这可能是一个很少偏离主题但这有助于我理解委员会上述理由背后的原因。



一种方法是分配每个函数分开的内存空间。有效地

你宣布每个可变的静态没有实际使用

关键字。这具有禁止递归函数的效果,并且还浪费内存,但它可以简化编译器。


Hi gurus

I was reading FAQ

"alloca cannot be written portably, and is difficult to implement on
machines without a conventional stack."

I understand that the standard does not mandate "heap" or "stack"

I''m curious to know the implemenations which dont have stack or heap.
How do they manage local and global memory? Although this may be a
little off-topic but this will help me understand the reasons behind
the above rationale of the committee.

I hope to get interesting answers since in my limited exposure I''ve
always seen machines with stack n heap and someone from this worldwide
forum would get me some answers about the machines who did not have
stack and/or heap and yet ran standard C.

Thanks in advance
-Sushil Ramachandran

解决方案

One example would be PIC controlers!
The PIC controler does have an internal hardware stack with only a few
(8-32) words depth. This hardware stack is commited only to call and returns
And there is no push/pop to the stack.
So my PIC C compilator has to put all my local ''C'' variables in the global
space!
Ofcousre there is also the lack of an OS handling the memory resourses!
''C'' leaves all that to the ''C'' implementor!
The ''C'' code looks more or less the same in a small PIC controller as it
looks in
a PC for example.
void ex()
{
int var;//In stack on a pc.//Global ram in a PIC
...
}
//jota


"jota" <ab*@hotmail.com> wrote in
news:MT******************@newsb.telia.net:

One example would be PIC controlers!
The PIC controler does have an internal hardware stack with only a few
(8-32) words depth. This hardware stack is commited only to call and
returns And there is no push/pop to the stack.
So my PIC C compilator has to put all my local ''C'' variables in the
global space!
This is a bit off-topic since it''s not to do with the C langauge but
implementations of C compilers. This would be far better discussed in
comp.arch.embedded.

<OT>
That''s not the reason. There''s no hardware stack on many 32-bit RISC CPUs
either. The just use one of the registers, by convention, to be the stack
pointer. Then, instead of pushes and pops you use load and stores with an
add or sub on the "stack pointer" register.
Ofcousre there is also the lack of an OS handling the memory resourses!
''C'' leaves all that to the ''C'' implementor!
The ''C'' code looks more or less the same in a small PIC controller as it
looks in
a PC for example.
void ex()
{
int var;//In stack on a pc.//Global ram in a PIC



It''s not global, it''s local to the function. It may or maynot be overlayed
with other local variables from other functions. This is also how the Keil
compiler works for the 8051. However, you loose reentrancy so in this mode
the compiler is not ISO C compliant. </OT>

--
- Mark ->
--



"Sushil" <in*******@yahoo.com> wrote in message

I''m curious to know the implemenations which dont have stack or
heap.
How do they manage local and global memory? Although this may be a
little off-topic but this will help me understand the reasons behind
the above rationale of the committee.


One way is to allocate separate memory space for each function. Effectively
you are declaring every varaible "static" without actually using the
keyword. This has the effect of banning recursive functions, and is also
wasteful of memory, but it may simplify the compiler.


这篇关于有关alloca()的问题以及在实现中使用堆栈的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆