12

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;
}

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

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.

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.

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.

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)?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 5
    I read 3.7/1 as saying "by definition, _storage duration_ specifies a minimum" and 3.7.2/1 as saying "the storage duration for an automatic variable is until the enclosing block exits". So I think there is no contradiction and either strategy is permissible. (Indeed, a garbage-collecting implementation that in reality keeps everything on the heap is probably permissible.) – Nemo Aug 18 '11 at 06:22

2 Answers2

7

I read 3.7/ as an introductory description and definition of the different storage classe (automatic, static, dynamic) and not as the implementation requirement for each... the implementation requirement for automatich is then described in 3.7.2/1 .

Reading 3.7.2/1 it does not forbid that it exists longer than the block exists (that is just the minimum) - IMHO this is an opening for compiler implementors regarding possible optimizations...

Yahia
  • 69,653
  • 9
  • 115
  • 144
6

"Lasts until" also is a minimum.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • So do I read those two as "the minimum duration is until the block ends"? – sharptooth Aug 18 '11 at 06:53
  • Correctly. Note that even if it would be otherwise, there _still_ would be no requirement for the compiler to reuse the storage location at the first possible opportunity. – MSalters Aug 18 '11 at 07:36