C89 includes blocks you know...
C89 includes blocks you know...
Posted Jun 11, 2008 10:23 UTC (Wed) by nix (subscriber, #2304)In reply to: C89 includes blocks you know... by Yorick
Parent article: Implications of pure and constant functions
Really? Everything I've found, including my reading of the text of C89, seems to say that initializers of automatic variables must be constant (and I use a number of compilers on a daily basis that reject non-constant initializers in C89 mode). A cite from C89 that supports your interpretation would be nice.
      Posted Jun 11, 2008 11:31 UTC (Wed)
                               by ballombe (subscriber, #9523)
                              [Link] 
       
     
      Posted Jun 12, 2008 5:17 UTC (Thu)
                               by eru (subscriber, #2753)
                              [Link] (1 responses)
       
Well, they are broken compilers. Complain to the vendor.
 
A cite from C89 that supports your interpretation would be nice.
 
From my photocopy the original X3.159-1989: 
Since the standard does not specify such constantness constraint for
all objects with automatic storage duration, they can be runtime values.
 
The Rationale section for 3.5.7 makes this even more clear: According to it, the committee even considered allowing automatic aggregate initializers to consist of series of or arbitrary runtime expressions, but did not
go that far in the end. The rationale also mentions that a function call
that returns a structure is permitted as an initializer for an automatic
variable with structure type. I have used some old compilers that had
problems with this kind of structure initialization, but they still
allowed automatic scalars to be initialized with runtime values. I think
even K&R C allowed this.
      
           
     
    
      Posted Jun 12, 2008 10:54 UTC (Thu)
                               by nix (subscriber, #2304)
                              [Link] 
       
     
    
      I cannot provide you with one, but C89 includes blocks you know...
      info gcc document this extension seems to imply the C89 restriction only apply to aggregate initializers.
5.18 Non-Constant Initializers
==============================
As in standard C++ and ISO C99, the elements of an aggregate
initializer for an automatic variable are not required to be constant
expressions in GNU C.  Here is an example of an initializer with
run-time varying elements:
     foo (float f, float g)
     {
       float beat_freqs[2] = { f-g, f+g };
       /* ... */
     }
      
          
      Really? Everything I've found, including my reading of the text of C89, seems to say that
initializers of automatic variables must be constant (and I use a number of compilers on a
daily basis that reject non-constant initializers in C89 mode).
C89 includes blocks you know...
      
3.5.7 Initialization
[syntax omitted]
Constraints
[...]
All the expressions in an initializer for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant
expressions.
C89 includes blocks you know...
      
OK, broken compilers it is. (It's surprising that a language as apparently simple as C can
still trip one up with unexpected corners like this after so long.)
 
           