admin管理员组

文章数量:1410674

I've read that C doesn't mandate a minimum stack size, leaving it implementation-defined. But could a conforming C compiler refuse to compile a program if it detects--say, through static analysis--that the call stack depth (e.g., from nested function call) exceeds a fixed limit it supports? Or is this always a runtime issue? I'm curious if the C standard (like C11 or C23) allow rejecting valid syntax at compile time based on stack depth alone, assuming no recursion or undefined behaviour

I've read that C doesn't mandate a minimum stack size, leaving it implementation-defined. But could a conforming C compiler refuse to compile a program if it detects--say, through static analysis--that the call stack depth (e.g., from nested function call) exceeds a fixed limit it supports? Or is this always a runtime issue? I'm curious if the C standard (like C11 or C23) allow rejecting valid syntax at compile time based on stack depth alone, assuming no recursion or undefined behaviour

Share Improve this question asked Mar 6 at 11:08 Alphin ThomasAlphin Thomas 1 8
  • §5.2.4.1 ¶1 of the C11 standard does not mention a minimum stack size or a minimum recursive depth. The same applies to the corresponding section of C23. – Andreas Wenzel Commented Mar 6 at 11:26
  • 1 The stack size allocated is a function of the OS, not of C. The C standard does not mention stack or heap. The stack size is decided at run time, not at compile time. – Weather Vane Commented Mar 6 at 11:43
  • Stack size is a linker task, --stack_size ..., so the pure C compiler should remain quiet. And as (cross compilation) you could link something not loadable on you machine, I tip on a runtime error when loading. – Joop Eggen Commented Mar 6 at 11:54
  • @JoopEggen: As far as I can tell, the C standard does not distinguish between a compiler and a linker. Therefore, I believe that your comment is only relevant from a practical point of view, but not from a language-lawyer point of view. – Andreas Wenzel Commented Mar 6 at 12:03
  • @AndreasWenzel I wanted to relativize the expectation that a C compiler with a sooo good static code analysis would use some "legal" rule of the language to give a warning. Though a stack allocation int a[9999...999]; would be worth a warning. You are more than correct though that I am not talking about a language specification. – Joop Eggen Commented Mar 6 at 12:45
 |  Show 3 more comments

2 Answers 2

Reset to default 7

The C standard is only concerned about limitations during compilation, meaning things like the amount of nested blocks in the source code. It does not mention hardware-related things like stack memory or call stack depth. Some of the more exotic targets which can you can write C code for don't even have a stack.

A conforming C compiler is only required to give diagnostic messages for constraint and syntax violations. There is nothing in the standard preventing a C compiler for doing extra diagnostics beyond that. And so compilers also tend to give messages for other forms of violations: semantics, explicit undefined behavior etc, but none of that is required by the standard.

Things like running out of memory at compile-time is rather the linker's business and linkers do tend to give diagnostics about that. But linkers aren't really covered by the C standard at all.

Similarly, the C standard does not state what will happen to an executable in case there were diagnostic messages during compilation, regardless if the messages were about non-conformance or something else.

The stack size is usually determined at runtime by the OS or hardware, and exceeding it leads to a stack overflow.

Recursive function calls or deeply nested function calls may trigger a stack overflow at runtime, but the compiler is not required to detect this

本文标签: