自动存储持续时间有什么具体要求? [英] What are exact requirements on automatic storage duration?

查看:149
本文介绍了自动存储持续时间有什么具体要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据编译器的以下代码:

Depending on the compiler the following code:

int main()
{
   srand( 0 );
   if( rand() ) {
      char buffer[600 * 1024] = {};
      printf( buffer );
   } else {
      char buffer[500 * 1024] = {};
      printf( buffer );
   }
   return 0;
}

当在最大堆栈大小等于1兆字节的系统上运行时,

when ran on a system with maximum stack size equal to 1 megabyte either prints an empty string or crashes with a stack overflow.

不同之处在于不同的编译器会以不同的方式分配自动存储空间。大多数编译器为函数start上的所有对象分配存储,因此在上面的代码中,它们分配了600 + 400 = 1100千字节,导致堆栈溢出。一些编译器是更聪明的,他们看到这两个数组永远不能同时访问,所以他们重用相同的内存,只分配600千字节,程序运行良好。

The difference is because different compilers allocate automatic storage differently. Most compilers allocate storage for all objects on function start, so in the code above they allocate 600+400=1100 kilobytes and that leads to stack overflow. Some compilers are smarter and they see that those two arrays can never be accessible at the same time so they reuse the same memory and only allocate 600 kilobytes and the program runs fine.

现在标准表示(3.7 / 1),存储持续时间定义存储的最小潜在寿命,然后(3.7.2 / 1)存储这些对象[具有自动持续时间]持续到创建它们的块存在

Now The Standard says (3.7/1) that storage duration defines the minimum potential lifetime of the storage and then (3.7.2/1) that the storage for these objects [with automatic duration] lasts until the block in which they are created exists.

我不明白如何应用3.7 / 1和3.7.2 / 1一起。一个人说持续时间是最小潜力,另一个人明确地说,持续到块存在。看起来像根据第一两个分配策略是合法的,但第二个要求只使用重用分配策略。

I don't understand how 3.7/1 and 3.7.2/1 are to be applied together. One says that duration is minimum potential and the other says explicitly that it lasts until the block exists. Looks like according to the first both allocation strategies are legal, but the second demands that only "reuse" allocation strategy is used.

如何3.7 / 1和3.7.2 / 1共存?

How do 3.7/1 and 3.7.2/1 co-exist? Is it legal to allocate more memory than the program needs in the worst case (the first strategy)?

推荐答案

持续到也是最低的。

这篇关于自动存储持续时间有什么具体要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆