diff options
author | rth <rth@138bc75d-0d04-0410-961f-82ee72b054a4> | 2001-01-04 01:14:16 +0000 |
---|---|---|
committer | rth <rth@138bc75d-0d04-0410-961f-82ee72b054a4> | 2001-01-04 01:14:16 +0000 |
commit | 15119d83f2aa9fdd955ab2cc85d3de072bd80dea (patch) | |
tree | cc93c1e7ae55e7da5ca7945790d3617cca46adb3 /gcc/extend.texi | |
parent | ae5c5d31fdc2f69d9e95f0bdf4c86e0f4a83f6b2 (diff) | |
download | gcc-15119d83f2aa9fdd955ab2cc85d3de072bd80dea.tar.gz |
* c-decl.c (grokdeclarator): Give zero-length arrays size zero.
Remove dead code.
* c-typeck.c (push_init_level): Move checks for flexible array
members and zero length arrays ...
(pop_init_level): ... here. Silently discard empty initializations.
Remove dead code.
* varasm.c (output_constructor): Update for sizeof change to
zero-length arrays.
* extend.texi (Zero Length): Clarify semantics.
* gcc.dg/940510-1.c: Update expected error wording.
* gcc.dg/array-2.c, gcc.dg/array-3.c, gcc.dg/array-4.c: New.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@38678 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/extend.texi')
-rw-r--r-- | gcc/extend.texi | 52 |
1 files changed, 37 insertions, 15 deletions
diff --git a/gcc/extend.texi b/gcc/extend.texi index 811a423e7db..1a0b0e14c73 100644 --- a/gcc/extend.texi +++ b/gcc/extend.texi @@ -869,8 +869,8 @@ extension for floating-point constants of type @code{float}. @cindex zero-length arrays @cindex length-zero arrays -Zero-length arrays are allowed in GNU C. They are very useful as the last -element of a structure which is really a header for a variable-length +Zero-length arrays are allowed in GNU C. They are very useful as the +last element of a structure which is really a header for a variable-length object: @example @@ -879,32 +879,54 @@ struct line @{ char contents[0]; @}; -@{ - struct line *thisline = (struct line *) - malloc (sizeof (struct line) + this_length); - thisline->length = this_length; -@} +struct line *thisline = (struct line *) + malloc (sizeof (struct line) + this_length); +thisline->length = this_length; @end example In ISO C89, you would have to give @code{contents} a length of 1, which means either you waste space or complicate the argument to @code{malloc}. -In ISO C99, you would use a @dfn{flexible array member}, which uses a -slightly different syntax: leave out the @code{0} and write -@code{contents[]}. +In ISO C99, you would use a @dfn{flexible array member}, which is +slightly different in syntax and semantics: + +@itemize @bullet +@item +Flexible array members are written as @code{contents[]} without +the @code{0}. + +@item +Flexible array members have incomplete type, and so the @code{sizeof} +operator may not be applied. As a quirk of the original implementation +of zero-length arrays, @code{sizeof} evaluates to zero. + +@item +Flexible array members may only appear as the last member of a +@code{struct} that is otherwise non-empty. GCC currently allows +zero-length arrays anywhere. You may encounter problems, however, +defining structures containing only a zero-length array. Such usage +is deprecated, and we recommend using zero-length arrays only in +places in which flexible array members would be allowed. -GCC allows static initialization of the zero-length array if -the structure is not nested inside another structure. I.e. +@item +GCC allows static initialization of the zero-length array if the structure +is not nested inside another structure. In addition, for backward +compatibility with an earlier versions of gcc, we allow a degenerate empty +initialization when nested inside another structure. I.e. @example +struct bar @{ struct line a; @}; + /* Legal. */ struct line x = @{ 4, @{ 'g', 'o', 'o', 'd' @} @}; /* Illegal. */ -struct bar @{ - struct line a; -@} y = @{ @{ 3, @{ 'b', 'a', 'd' @} @} @}; +struct bar y = @{ @{ 3, @{ 'b', 'a', 'd' @} @} @}; + +/* Legal. */ +struct bar z = @{ @{ 0, @{ @} @} @}; @end example +@end itemize @node Variable Length @section Arrays of Variable Length |