将在C中无限循环中的可变声明导致堆栈溢出

发布于 2025-02-11 16:51:17 字数 689 浏览 3 评论 0原文

这是一个简单的问题,但是我想把它扔到那里,并感谢任何人是否可以验证我的理解是否正确或提供更多见解。如果这是一个重复的帖子,我的事先表示歉意。

例如。在下面的代码中:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羞稚 2025-02-18 16:51:18

事实并非如此。

每次输入循环的主体时,都会在堆栈上创建两个变量。当循环击中结束}时,这些变量然后被破坏。测试循环条件并重新输入身体后,创建了新的变量。

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.

不必了 2025-02-18 16:51:18

我是否正确地说,在第一个代码段中,此代码最终会导致堆栈溢出,因为无限循环内部不断声明的变量?

不:

6.2.4对象的存储持续时间
...
6对于没有可变长度阵列类型的对象,其寿命从进入与之关联的块直到该块以任何方式结束 延伸。 (输入一个封闭的块或调用函数暂停,但不会结束,执行当前块。)如果递归输入块,则每次都会创建对象的新实例。对象的初始值不确定。 如果为对象指定了初始化,则每次在执行块的执行中达到声明或复合文字时都会执行。否则,每次达到声明

时,该值将不确定
7对于确实具有可变长度阵列类型的对象,其寿命从对象的声明延伸到程序执行之前,请留下声明的范围。 35)如果递归输入范围,每次都会创建一个对象的新实例。对象的初始值不确定。


35)离开包含声明的最内向块,或在声明之前跳到该块或嵌入式块中的一个点,离开声明的范围。

C 2011 Online Draft

每次通过循环,some_intsome_pointer的新实例将在循环主体的开头创建,并在其结束时被摧毁 - 无论如何,从逻辑上讲。在我使用的大多数实现中,这些项目的存储将在功能输入时分配一次,并在功能退出时发布。但是,这是一个实现细节,尽管很常见,但您不应该依靠它到处都是真实的。

如果some_function动态分配内存或其他在循环结束之前未被释放的资源,则可以耗尽动态内存池或其他方式,但这并不应该导致堆栈溢出。

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?

No:

6.2.4 Storage durations of objects
...
6 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

7 For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration.35) If the scope is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate.


35) Leaving the innermost block containing the declaration, or jumping to a point in that block or an embedded block prior to the declaration, leaves the scope of the declaration.

C 2011 Online Draft

Each time through the loop new instances of some_int and some_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.

Saygoodbye 2025-02-18 16:51:18

通常不是,但这不是最好的做法。
但是,如果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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文