直接堆栈和堆访问;虚拟级别还是硬件级别? [英] Direct stack and heap access; Virtual- or hardware- level?

查看:78
本文介绍了直接堆栈和堆访问;虚拟级别还是硬件级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我参加SO时,我阅读了很多指导性的评论(特别是在C语言中)

When I'm on SO I read a lot of comments guiding (Especially in C)

动态分配始终进入堆,在堆栈上自动分配"

"dynamic allocation allways goes to the heap, automatic allocation on the stack"

但是特别是对于纯C语言,我不同意这一点.由于ISO/IEC9899甚至不会丢一堆字.它只提到了三种存储方式(静态,自动和分配),并建议了每种方式的处理方式.

But especially regarding to plain C I disaggree with that. As the ISO/IEC9899 doesn't even drop a word of heap or stack. It just mentions three storage duriations (static, automatic, and allocated) and advises how each of them has to be treat.

如果愿意的话,将使编译器可以选择执行此操作,反之亦然.

What would give a compiler the option to do it even wise versa if it would like to.

所以我的问题是:

(即使不是在C语言中)标准化语言可以说"...必须在堆上发生,...在堆栈上发生"的堆和堆栈物理存在吗?

Are the heap and the stack physical existing that (even if not in C) a standardized language can say "... has to happen on heap and ... on the stack"?

或者它们仅仅是管理内存访问的一种完善的系统,以致语言无法制定规则,因为甚至无法确保环境是否支持它们?

Or are they just a virtuell system of managing memory access so that a language can't make rules about them, as it can't even be ensured the enviroment supports them?

在我的知识库中,只有第二个才有意义.但是我读过很多遍,很多人都在写评论,例如在XY语言中,这将发生在堆栈/堆上".但是,如果我是对的,那么这必须是不确定的,只要该语言不是仅用于保证具有堆栈和堆的系统即可.而且所有的评论都将是错误的.

In my knowledgebase only the second would make sense. But I read allready many times people writing comments like "In language XY this WILL happen on the stack/heap". But if I'm right this had to be indeterminable as long the language isn't just made for such systems which guarantee to have a stack and heap. And all thoose comments would be wrong.

这就是让我问这个问题的原因.我是错了吗,还是在推理方面有很大的错误?

Thats what lead me to ask about this question. Am I that wrong, or is there a big error in reasoning going around about that?

推荐答案

您是正确的,只要C规范不需要正确地实现存储类,就不需要使用堆或栈.

You are correct in that the C spec doesn't require the use of a heap or stack, as long as it implements the storage classes correctly.

但是,实际上,每个编译器都会将堆栈用于自动变量,将堆用于分配的变量.虽然您可以实现不使用堆栈或堆的编译器,但是它可能无法很好地运行,并且对于大多数开发人员来说都不是熟悉的.

However, virtually every compiler will use stacks for automatic variables and heaps for allocated variables. While you could implement a compiler that doesn't use a stack or heap, it probably wouldn't perform very well and wouldn't be familiar to most devs.

因此,当人们说总是"时,他们实际上的意思是几乎总是".

So when people say "always", they really mean "virtually always".

这篇关于直接堆栈和堆访问;虚拟级别还是硬件级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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