没有 Stackoverflow:while 循环内的自动对象

发布于 2024-09-15 09:24:15 字数 637 浏览 6 评论 0原文

我在查看某人的代码时遇到了一个线程:

while(TRUE)

{
 ......
 STRUCT_MSG_SYS_HEADER  sysHdr;
 .....
 ....
}

有五个这样的线程,我的观点是“STRUCT_MSG_SYS_HEADER sysHdr;”一段时间或几天后会导致 stackoverflow...(虽然未经测试)。所以我决定编写一个简单的示例应用程序,

  1 #include "stdio.h"
  2
  3 struct infinite
  4 {
  5     int arr[1000000];
  6 }infinite;
  7
  8 int main()
  9 {
 10     while(1)
 11     {
 12         struct infinite infobj;
 13         printf("\ninfinite = %x\n", &infobj);
 14     }
 15     return 0;
 16 }

但这里它打印的是 infobj 的相同地址。是我对stackoverflow的想法是错误的还是这里编译器做了一些优化? (我认为自己是一名优秀的程序员,但类似的事情迫使我重新思考,再次阅读丹尼斯·里奇)

I was going through someone's code where I came across a thread:

while(TRUE)

{
 ......
 STRUCT_MSG_SYS_HEADER  sysHdr;
 .....
 ....
}

There are five threads like this, My point is that "STRUCT_MSG_SYS_HEADER sysHdr;" will lead to stackoverflow after some time or days... (Not tested though). So I decided to write a simple sample application

  1 #include "stdio.h"
  2
  3 struct infinite
  4 {
  5     int arr[1000000];
  6 }infinite;
  7
  8 int main()
  9 {
 10     while(1)
 11     {
 12         struct infinite infobj;
 13         printf("\ninfinite = %x\n", &infobj);
 14     }
 15     return 0;
 16 }

But here it is printing the same address for infobj. Is my thinking of stackoverflow is wrong or here compiler has done some optimization? (I consider myself good coder, but things like these force me to think again, read dennis richie again)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

笑忘罢 2024-09-22 09:24:15

infobj 在 while 循环的每次迭代结束时被销毁,因此堆栈不会溢出,并且您会一次又一次地获得相同的地址。是否可以在堆栈上分配int arr[1000000]取决于每个线程允许的最大堆栈大小。在 VC 编译器上,该大小为 1 MB,但可以通过编译器选项进行更改。

The infobj is destroyed at the end of each iteration of while loop, hence stack is not overflowing and you are getting the same address again and again. Whether you can allocate int arr[1000000] on the stack depends on the maximum allowed stack size per thread. On VC compiler this is 1 MB but can be changed through compiler options.

卖梦商人 2024-09-22 09:24:15

不会发生堆栈溢出。该堆栈分配的变量仅在循环迭代期间有效,之后它占用的堆栈空间可以被重用。通常,当下一次迭代开始时,新迭代中的变量将使用完全相同的空间。

如果对同一函数进行深度递归调用,则可能会发生堆栈溢出,因为当新调用开始时,前一个调用范围内的所有变量都需要保留,因此空间不会被重用,而是更多堆栈空间将用于每个新调用。在这个问题中情况并非如此。

No stack overflow will occur. That stack-allocated variable will only live for the duration of the loop iteration and after that stack space it occupies can be reused. Typically when the next iteration starts exactly the same space will be used for the variable in the new iteration.

Stack overflow could occur in case of deep recursive chain of calls of the same function because when the new call would start all the variables in the scope of the previous call would need to be retained, so the sapce would not be reused, but instead more stack space would be used for each new call. This is not the case in this question.

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