Does the C language specify a stack? [duplicate]

余生颓废 提交于 2020-01-16 17:36:15

问题


Does the C language definition require the existence of a stack? What about architectures that don't support stacks? Does that mean that such architectures can't implement the C language as it is defined?

What about the heap?


回答1:


No.

The C11 standard does not contain the word stack, nor does it contain the word heap. That means it does not require either by name.

If an architecture doesn't have a stack, it must still have a mechanism to allow a called function to allocate space for its variables, even if it is called recursively. As long as the CPU permits the compiler writer to implement such a system somehow, the CPU can support C. A stack is an easy way to handle per-function variable allocation, but it is far from being the only possible mechanism.

If you're in a hosted implementation (as opposed to a freestanding implementation), then the implementation is required to support dynamic memory allocation via malloc(), free() and friends. That is normally done by using space on 'the heap', but the standard doesn't stipulate how it must happen, only that it must be managed appropriately for the platform.

A lot of the standard's rules that seem bizarre are there precisely to make it possible for unusual CPU architectures to run standard C.



来源:https://stackoverflow.com/questions/54067647/does-the-c-language-specify-a-stack

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!