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 | Show 3 more comments2 Answers
Reset to default 7The 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
本文标签:
版权声明:本文标题:compilation - Can a C compiler legally reject a program if its call stack depth exceeds a fixed limit at compile time? - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744980849a2635805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
--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:54int 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