将在C中无限循环中的可变声明导致堆栈溢出
这是一个简单的问题,但是我想把它扔到那里,并感谢任何人是否可以验证我的理解是否正确或提供更多见解。如果这是一个重复的帖子,我的事先表示歉意。
例如。在下面的代码中:
(1)stack_overflow.c
int main() {
while (1) {
int some_int = 0;
char* some_pointer = some_function();
// ... some other code ....
}
}
(2)no_overflow.c
int main() {
int some_int;
char* some_pointer;
while (1) {
some_int = 0;
some_pointer = some_function();
// ... some other code ....
}
}
am我正确地说,在第一代码段中,此代码最终会导致堆栈溢出,因为无限循环内部不断声明变量? (无限循环是有意的)
,而在第二个代码段中,我们实际上将重复使用相同的内存,这是所需的结果,因为它将更有效。
编译器优化是否能够检测到这一点并防止堆栈溢出,如果是的,哪个compilers&优化水平会实现这一目标吗?
This is a simple question, but I would just like to throw this out there and appreciate if anyone could validate if my understanding is correct or provide some more insight. My apologies in advance if this is a duplicate post.
Eg. In the code below:
(1) stack_overflow.c
int main() {
while (1) {
int some_int = 0;
char* some_pointer = some_function();
// ... some other code ....
}
}
(2) no_overflow.c
int main() {
int some_int;
char* some_pointer;
while (1) {
some_int = 0;
some_pointer = some_function();
// ... some other code ....
}
}
Am I correct in saying that in the 1st code snippet, this code would eventually cause a stack overflow because of the variables continually being declared inside of the infinite loop? (The infinite loop is intentional)
Whereas, in the second code snippet we would actually be reusing the same parts of memory, which is the desired outcome as it would be more efficient.
Would compiler optimisation be able to detect this and prevent the stack overflow, if so which c compilers & optimisation levels would achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实并非如此。
每次输入循环的主体时,都会在堆栈上创建两个变量。当循环击中结束
}
时,这些变量然后被破坏。测试循环条件并重新输入身体后,创建了新的变量。This is not the case.
Each time the body of the loop is entered, two variables are created on the stack. Those variables are then destroyed when the loop hits the ending
}
. After the loop condition is tested and the body is reentered, a new pair of variables are created.不:
C 2011 Online Draft
每次通过循环,
some_int
和some_pointer
的新实例将在循环主体的开头创建,并在其结束时被摧毁 - 无论如何,从逻辑上讲。在我使用的大多数实现中,这些项目的存储将在功能输入时分配一次,并在功能退出时发布。但是,这是一个实现细节,尽管很常见,但您不应该依靠它到处都是真实的。如果
some_function
动态分配内存或其他在循环结束之前未被释放的资源,则可以耗尽动态内存池或其他方式,但这并不应该导致堆栈溢出。No:
C 2011 Online Draft
Each time through the loop new instances of
some_int
andsome_pointer
will be created at the beginning of the loop body and destroyed at the end of it - logically speaking, anyway. On most implementations I've used, storage for those items will be allocated once at function entry and released at function exit. However, that's an implementation detail, and while it's common you shouldn't rely on it being true everywhere.If
some_function
dynamically allocates memory or some other resource that doesn't get freed before the end of the loop you could exhaust your dynamic memory pool or whatever, but it shouldn't cause a stack overflow as such.通常不是,但这不是最好的做法。
但是,如果
some_function()
是在每个调用中分配一个变量,并且它不会在同一循环中释放,则您将失去分配变量的位置并具有内存错误。请分享其余的代码以获得更清晰的答案。
normally no but it's not the best practice.
however, if
some_function()
is allocating a variable with every call, and it's not being freed in the same loop, you will lose the location of your allocated variable and have memory errors.please share the rest of the code for a clearer answer.