원문(StackOverflow) : Does C need a stack and a heap in order to run?
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?
** 사람들은 heap 메모리와 stack 메모리가 무엇인지, 차이는 무엇인지 말한다. 근데 내가 궁금한건 CPU가 힙과 스택을 지원하지 않는다면 , C는 올바르게 동작할 수 있나 ?
No, it does not. Let's cover the heap first, that's easy.
필요없다. 힙이 더 쉬우니까 힙을 먼저 생각해보자.
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.
힙을 제공하지 않는 어떤 구현이든 단지 malloc(아니면 다른 어떤 메모리 할당 함수) 을 호출 했을때 NULL을 return 할 필요가 있다. 그것은 표준에 따르면 충분히 수용가능한 동작이다.
In terms of the stack, it also doesn't need to provide one. ISO C11 mentions the word "stack" exactly zero times.
스택은, 마찬가지로 제공할 필요가 없다. ISO C11 은 Stack 이라는 단어를 단 한번도 언급하지 않는다.
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.
구현이 필요한 것은 표준에 명세된 모든것을을 위해 단순히 올바른(정답인) "가상 머신"이 되는 것이다. Stack을 사용하지 않는다면 매우 어렵겠지만 불가능한것은 아니다. 극단적인 케이스로, 모든 함수를 인라인 함수로 짜고 재귀적으로 호출하는게 불가능하다라고 말하지 않는다. 상대적으로 더 많은 코드와 함수-구체적인 데이터 공간을 사용하겠지만 가능하다.
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).
그러나 이런것들은 스택을 지원하는(그리고 힙이나 다른 것들) 다른 구조로 옮길 확신을 주는 것이다.
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.
위에 따라 아키텍처가 힙이나 스택을 지원하지 않아도 기본 메모리 I/O 동작으로 구현 될 수 있다. 사실 내가 청소년기에 가졌던 초기의 컴퓨터중에 하나인 RCA 1802는 스택도 없었고 call 이랑 ret instruction 도 없었다.
이하 생략.