1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

6

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278