public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/39383]  New: sizeof object with zero-length array ignores initializer
@ 2009-03-05 12:39 algrant at acm dot org
  2009-03-05 13:06 ` [Bug c/39383] " ebotcazou at gcc dot gnu dot org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: algrant at acm dot org @ 2009-03-05 12:39 UTC (permalink / raw)
  To: gcc-bugs

Example from docs: 
  struct f1 { int x; int y[]; } f1 = { 1, { 2, 3, 4 } };
Currently sizeof(f1) == sizeof(int) rather than 4*sizeof(int), i.e.
sizeof the object is determined by the type only, not the actual object
size taking into account its initializer.  If this is intentional the
docs could benefit from clarification, as otherwise it's natural to
want to use sizeof to find out how big the object actually ended up as.


-- 
           Summary: sizeof object with zero-length array ignores initializer
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: algrant at acm dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
@ 2009-03-05 13:06 ` ebotcazou at gcc dot gnu dot org
  2009-03-05 13:19 ` algrant at acm dot org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: ebotcazou at gcc dot gnu dot org @ 2009-03-05 13:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from ebotcazou at gcc dot gnu dot org  2009-03-05 13:06 -------
> Currently sizeof(f1) == sizeof(int) rather than 4*sizeof(int), i.e.
> sizeof the object is determined by the type only, not the actual object
> size taking into account its initializer.

That's C.  6.5.3.4 (2) reads:

2 The sizeof operator yields the size (in bytes) of its operand, which may be
an expression or the parenthesized name of a type. The size is determined from
the type of the operand. The result is an integer. If the type of the operand
is a variable length array type, the operand is evaluated; otherwise, the
operand is not evaluated and the result is an integer constant.


-- 

ebotcazou at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ebotcazou at gcc dot gnu dot
                   |                            |org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
  2009-03-05 13:06 ` [Bug c/39383] " ebotcazou at gcc dot gnu dot org
@ 2009-03-05 13:19 ` algrant at acm dot org
  2009-03-05 13:31 ` ebotcazou at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: algrant at acm dot org @ 2009-03-05 13:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from algrant at acm dot org  2009-03-05 13:19 -------
No, the case is an extension to C.  6.5.3.4 was obviously written without
this case in mind.  In this case "the size... of its operand" cannot be
"determined from the type".  Either sizeof doesn't return the (real) size
of the operand, or it isn't determined from the type.  Current GCC behavior
is to have it not return the size of the operand.  That's a valid point of 
view but it does mean one needs to take care when using sizeof on such
objects, and it's worth documenting.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
  2009-03-05 13:06 ` [Bug c/39383] " ebotcazou at gcc dot gnu dot org
  2009-03-05 13:19 ` algrant at acm dot org
@ 2009-03-05 13:31 ` ebotcazou at gcc dot gnu dot org
  2009-03-05 13:53 ` joseph at codesourcery dot com
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: ebotcazou at gcc dot gnu dot org @ 2009-03-05 13:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from ebotcazou at gcc dot gnu dot org  2009-03-05 13:31 -------
> No, the case is an extension to C.  6.5.3.4 was obviously written without this
> case in mind.

Not at all, see 6.7.2.1 (16):

16 As a special case, the last element of a structure with more than one named
member may have an incomplete array type; this is called a flexible array
member. With two exceptions, the flexible array member is ignored. First,
the size of the structure shall be equal to the offset of the last element of
an otherwise identical structure that replaces the flexible array member
with an array of unspecified length.106)


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (2 preceding siblings ...)
  2009-03-05 13:31 ` ebotcazou at gcc dot gnu dot org
@ 2009-03-05 13:53 ` joseph at codesourcery dot com
  2009-03-05 14:48 ` algrant at acm dot org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: joseph at codesourcery dot com @ 2009-03-05 13:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from joseph at codesourcery dot com  2009-03-05 13:53 -------
Subject: Re:  sizeof object with zero-length array ignores
 initializer

On Thu, 5 Mar 2009, ebotcazou at gcc dot gnu dot org wrote:

> ------- Comment #3 from ebotcazou at gcc dot gnu dot org  2009-03-05 13:31 -------
> > No, the case is an extension to C.  6.5.3.4 was obviously written without this
> > case in mind.
> 
> Not at all, see 6.7.2.1 (16):

Standard C does not permit initializing the flexible array member.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (3 preceding siblings ...)
  2009-03-05 13:53 ` joseph at codesourcery dot com
@ 2009-03-05 14:48 ` algrant at acm dot org
  2009-03-06 14:45 ` algrant at acm dot org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: algrant at acm dot org @ 2009-03-05 14:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from algrant at acm dot org  2009-03-05 14:48 -------
*** Bug 39384 has been marked as a duplicate of this bug. ***


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (4 preceding siblings ...)
  2009-03-05 14:48 ` algrant at acm dot org
@ 2009-03-06 14:45 ` algrant at acm dot org
  2009-03-06 17:27 ` jsm28 at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: algrant at acm dot org @ 2009-03-06 14:45 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from algrant at acm dot org  2009-03-06 14:44 -------
Right.  The existence of a statically initialized object whose size can
be determined at compile-time to be larger than the (notional) type size,
is specific to GCC.  Here's another thing: the assembler output shows

        .type   f1, %object
        .size   f1, 4
f1:
        .word   1
        .word   2
        .word   3
        .word   4

Whatever you think about sizeof(), surely the object's symbol should
have its true size (16 in this case)?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (5 preceding siblings ...)
  2009-03-06 14:45 ` algrant at acm dot org
@ 2009-03-06 17:27 ` jsm28 at gcc dot gnu dot org
  2009-04-08 21:36 ` b07584 at freescale dot com
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jsm28 at gcc dot gnu dot org @ 2009-03-06 17:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from jsm28 at gcc dot gnu dot org  2009-03-06 17:27 -------
Reopening.


-- 

jsm28 at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (6 preceding siblings ...)
  2009-03-06 17:27 ` jsm28 at gcc dot gnu dot org
@ 2009-04-08 21:36 ` b07584 at freescale dot com
  2009-04-08 22:54 ` joseph at codesourcery dot com
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: b07584 at freescale dot com @ 2009-04-08 21:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from b07584 at freescale dot com  2009-04-08 21:36 -------
> Standard C does not permit initializing the flexible array member.
> 

 However, (using revision 145360) with -std=c99, the given code compiles
 - no error is issued.


-- 

b07584 at freescale dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b07584 at freescale dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (7 preceding siblings ...)
  2009-04-08 21:36 ` b07584 at freescale dot com
@ 2009-04-08 22:54 ` joseph at codesourcery dot com
  2009-04-17 22:39 ` anmol at freescale dot com
  2009-04-23 15:53 ` anmol at freescale dot com
  10 siblings, 0 replies; 12+ messages in thread
From: joseph at codesourcery dot com @ 2009-04-08 22:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from joseph at codesourcery dot com  2009-04-08 22:54 -------
Subject: Re:  sizeof object with zero-length array ignores
 initializer

On Wed, 8 Apr 2009, b07584 at freescale dot com wrote:

> ------- Comment #8 from b07584 at freescale dot com  2009-04-08 21:36 -------
> > Standard C does not permit initializing the flexible array member.
> > 
> 
>  However, (using revision 145360) with -std=c99, the given code compiles
>  - no error is issued.

If you want an error use -pedantic-errors.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (8 preceding siblings ...)
  2009-04-08 22:54 ` joseph at codesourcery dot com
@ 2009-04-17 22:39 ` anmol at freescale dot com
  2009-04-23 15:53 ` anmol at freescale dot com
  10 siblings, 0 replies; 12+ messages in thread
From: anmol at freescale dot com @ 2009-04-17 22:39 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from anmol at freescale dot com  2009-04-17 22:39 -------
I am working on this problem and see that for generic ELF OS's,
defining ASM_DECLARE_OBJECT_NAME (gcc/config/elfos.h) to use the
size of the initializer (as against the size of the type) to emit
the true size in the .size directive results in the following code
being emitted for the submitted example:

        .type   f1, @object
        .size   f1, 16
f1:
        .long   1
        .long   2
        .long   3
        .long   4
        .section        .rodata

I am currently regression testing my fix (and plan
on submitting the patch for the generic ELF OS case
(assuming that this is the right approach to take
here)).

However, I am not sure if the same change will be
needed in ASM_FINISH_DECLARE_OBJECT as well? 
rest_of_decl_compilation in gcc/passes.c seems to
suggest that this macro is used for tentative
definitions - in which case there would be no 
initializer and that seems to suggest that no change
would be needed (using the size of the type would
be the right thing to do). But in the GCC Internals
Manual, Ch. 17 Sec. 17.21.4 Output and Generation of
Labels - the description suggests that a change is
needed in this macro as well. (Kindly advise, and I
shall incorporate).

Thank you.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/39383] sizeof object with zero-length array ignores initializer
  2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
                   ` (9 preceding siblings ...)
  2009-04-17 22:39 ` anmol at freescale dot com
@ 2009-04-23 15:53 ` anmol at freescale dot com
  10 siblings, 0 replies; 12+ messages in thread
From: anmol at freescale dot com @ 2009-04-23 15:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from anmol at freescale dot com  2009-04-23 15:53 -------

 Fix (for generic ELF systems) and test program for regression suite posted
here:

 http://gcc.gnu.org/ml/gcc-patches/2009-04/msg01807.html


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39383


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2009-04-23 15:53 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-05 12:39 [Bug c/39383] New: sizeof object with zero-length array ignores initializer algrant at acm dot org
2009-03-05 13:06 ` [Bug c/39383] " ebotcazou at gcc dot gnu dot org
2009-03-05 13:19 ` algrant at acm dot org
2009-03-05 13:31 ` ebotcazou at gcc dot gnu dot org
2009-03-05 13:53 ` joseph at codesourcery dot com
2009-03-05 14:48 ` algrant at acm dot org
2009-03-06 14:45 ` algrant at acm dot org
2009-03-06 17:27 ` jsm28 at gcc dot gnu dot org
2009-04-08 21:36 ` b07584 at freescale dot com
2009-04-08 22:54 ` joseph at codesourcery dot com
2009-04-17 22:39 ` anmol at freescale dot com
2009-04-23 15:53 ` anmol at freescale dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).