C是否需要堆栈和堆才能运行? [英] Does C need a stack and a heap in order to run?

查看:95
本文介绍了C是否需要堆栈和堆才能运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们谈论什么是栈和堆以及它们之间的区别.但是我很好奇,如果CPU不支持堆栈和堆结构,那么C可以在没有堆栈和堆的情况下正常运行吗?

People talk about what the stack and heap are and the differences between them. But I am curious to know that if a CPU does not support stack and heap structure, then can C run properly without a stack and a heap?

推荐答案

不,不是.让我们先覆盖一下堆,很简单.

No, it does not. Let's cover the heap first, that's easy.

当您尝试调用malloc(或任何其他内存分配函数)时,不提供任何种类的堆的实现只需要返回NULL.根据标准,这是完全可以接受的行为.

An implementation that does not provide a heap of any sort just needs to return NULL whenever you try to call malloc (or any other memory allocation function). That's perfectly acceptable behaviour according to the standard.

就堆栈而言,它也不需要提供一个. ISO C11完全零次提到堆栈"一词.

In terms of the stack, it also doesn't need to provide one. ISO C11 mentions the word "stack" exactly zero times.

实现 需要做的只是简单地为标准中指定的所有内容提供一个正确的虚拟机".当然,没有堆栈将非常困难,但这并非没有可能.在极端情况下,没有什么可以说您不能简单地递归地内联每个函数调用.那会使用大量的代码和特定于函数的数据空间,但这当然是可行的.

What an implementation does need to do is simply be a correct "virtual machine" for all the things specified in the standard. Granted that will be very difficult without a stack but it's not impossible. As an extreme case, there's nothing that says you can't simply inline every single function call recursively. That would use rather a large amount of code and function-specific data space, but it's certainly doable.

但是,可能是有一种说服力使我转向了另一种架构,即 did 具有堆栈(就此而言,还有堆).

However, it's probably something that would convince me to move to another architecture, one that did have a stack (and heap, for that matter).

已经说过,即使架构既不提供堆也没有提供堆栈,这两者都可以在基本的内存I/O操作中构建.实际上,我十几岁的时候最早使用的计算机之一就是RCA 1802 CPU,它没有专用的堆栈.它甚至没有callret指令.

Having said that, even if an architecture provides neither a heap nor a stack, both of those can be built out of basic memory I/O operations. In fact, one of the earliest computers I ever had as a teen sported an RCA 1802 CPU which had no dedicated stack. It didn't even have a call or ret instruction.

然而,使用SCRT(标准调用和返回技术),它可以很好地处理子例程和堆栈(对于"well"一词的某些定义).请参见此处详细了解这种美丽事物(或怪异的事物,取决于您的观点)以及其他一些不寻常的建筑.

Yet it could handle subroutines and a stack quite well (for some definition of the word "well") using its SCRT (standard call and return technique). See here for some more detail on how this thing of beauty (or monstrosity, depending on your viewpoint) worked, along with some other unusual architectures.

IBM Z(又称System z,zSeries,无论他们本周称其为什么)实际上都有一个堆(各种各样,可以从OS分配内存),但是没有堆栈.实际上,它通过使用此堆内存以及某些寄存器(类似于以上链接中引用的RCA芯片)来实现链接列表堆栈,这意味着函数序言使用STORAGE OBTAIN分配局部函数存储器,而Epilog使用STORAGE OBTAIN释放它. c5>.

The IBM Z (a.k.a. System z, zSeries, whatever they're calling it this week) actually has a heap (of sorts, in that you can allocate memory from the OS) but no stack. It actually implements a linked-list stack by using this heap memory along with certain registers (similar to the RCA chip referenced in the above link), meaning that a function prolog allocates local function memory using STORAGE OBTAIN and the epilog releases it with STORAGE RELEASE.

无需赘言,每个功能的序言和结尾都添加了很多额外的代码.

Needless to say that puts quite a bit of extra code into the prolog and epilog for each function.

这篇关于C是否需要堆栈和堆才能运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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