23

gcc 4.6によって有効なC(C99、C11)として扱われるVLA(可変長配列)を使用する奇妙なコードがいくつかあります。

$ cat a.c
int main(int argc,char**argv)
{
  struct args_t{
     int a;
     int params[argc];        // << Wat?
                        // VLA in the middle of some struct, between other fields
     int b;
  } args;

  args.b=0;

  for(args.a=0;args.a<argc;args.a++)
  {
    args.params[args.a]=argv[0][0];
    args.b++;
  }
  return args.b;
}

このコードは警告なしにコンパイルされました:

$ gcc-4.6 -Wall -std=c99 a.c && echo $?
0
$ ./a.out ; echo $?
1
$ ./a.out 2; echo $?
2
$ ./a.out 2 3; echo $?
3

同じ-std=c1x

$ gcc-4.6 -Wall -std=c1x a.c && echo $?
0

ただし、これはIntelCコンパイラまたはClang+LLVMでは機能しません。

$ icc a.c -o a.icc
a.c(5): warning #1361: variable-length array field type will be treated as zero-length array field type
       int params[argc];
                  ^
$ ./a.icc; echo $?
47

$ clang a.c -o a.clang
a.c:5:10: error: fields must have a constant size: 'variable length array in structure' extension will never be supported
     int params[argc];
         ^
1 error generated.

それで:

  1. これがGCCによって有効であると見なされるのはなぜですか?
  2. GCCの拡張である場合、どこに記述されていますか?
  3. C99およびC11ISO規格で有効ですか?
4

3 に答える 3

9

GCC does not allow it, compile with -std=c99 -pedantic-errors. A VLA inside a struct is apparently a (poorly documented) non-standard GNU C feature. See this.

于 2013-01-31T15:48:26.607 に答える
4

The standard is pretty clear that VLAs are not allowed in a struct:

6.7.2.1 Structure and union specifiers

9 - A member of a structure or union may have any complete object type other than a variably modified type. [...]

Variably modified types are (as you might expect) those derived from a variable length array (e.g. by adding array dimensions or cv qualifiers):

6.7.6 Declarators

3 - [...] If, in the nested sequence of declarators in a full declarator, there is a declarator specifying a variable length array type, the type specified by the full declarator is said to be variably modified. Furthermore, any type derived by declarator type derivation from a variably modified type is itself variably modified.

于 2013-01-31T16:20:08.077 に答える
-2

The authors of the C89 Standard recognized that many implementations implemented useful features which might be impractical on other implementations, and recognized that as a good thing. The Standard was intended as a minimum set of requirements for implementations; it was never intended to discourage implementations from providing features beyond that.

The Standard requires that if a conforming implementation allows a variable-length array to be declared within a structure defined at block scope, it must either document such behavior as an extension or issue a diagnostic when code contains such a declaration. Since an implementation would be free to process the code however it likes after issuing such a diagnostic, whether or not it documents an extension, the requirement to document extensions can only be meaningfully applied to extensions which do not generate diagnostics. That in turn would suggest that such things must be allowable.

The Standard does require that extensions not adversely affect the behavior of any Strictly Conforming programs, but since no such program could contain a VLA declaration within a structure that requirement is not a problem here.

于 2017-03-06T18:04:23.693 に答える