public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/25274]  New: sizeof(struct) fails for 'unlucky' structure
@ 2005-12-06 12:43 warg0d at go2 dot pl
  2005-12-06 13:08 ` [Bug c/25274] " rguenth at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: warg0d at go2 dot pl @ 2005-12-06 12:43 UTC (permalink / raw)
  To: gcc-bugs

the bug is in sizeof(element) - i've made unlucky structure, some
time ago, where sum of sizeof(each_element) gives size of 14 and
sizeof(whole_struct) returns 16. on old gcc-2.9.x there was the same
problem.
as an example program i've wrote shor one, that shows sizeof() from
struct having 14 char's table, 'unlucky' struct and sum of all elements sizes.

// sizeof(a)==16B instead of 14B...
struct a
{
 char           bfType[2];// 2B
 unsigned long  bfSize;// 4B
 unsigned short bfReserved1;// 2B
 unsigned short bfReserved2;// 2B
 unsigned long  bfOffBits;// 4B
};


// sizeof(b)==14B - OK!
struct b
{
 char nothing[14];
};


-- 
           Summary: sizeof(struct) fails for 'unlucky' structure
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: warg0d at go2 dot pl
 GCC build triplet: x86
  GCC host triplet: x86
GCC target triplet: x86


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


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

* [Bug c/25274] sizeof(struct) fails for 'unlucky' structure
  2005-12-06 12:43 [Bug c/25274] New: sizeof(struct) fails for 'unlucky' structure warg0d at go2 dot pl
@ 2005-12-06 13:08 ` rguenth at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2005-12-06 13:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2005-12-06 13:08 -------
The C standard allows for arbitrary padding in between elements, so this
behavior is expected.  Use __attribute__((packed)) on the type to get a struct
without padding if you need that for whatever reason (it will be slower to
access unaligned fields).  You can also sort the fields starting with elements
with
the largest size to minimize required padding.


-- 

rguenth at gcc dot gnu dot org changed:

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


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


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

end of thread, other threads:[~2005-12-06 13:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-06 12:43 [Bug c/25274] New: sizeof(struct) fails for 'unlucky' structure warg0d at go2 dot pl
2005-12-06 13:08 ` [Bug c/25274] " rguenth at gcc dot gnu dot org

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