This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/13078] New: sizeof returns incorrect 4 byte aligned value when structure contains integers and smaller types


Interesting and annoying bug... 
gcc version 3.2.3 Gentoo Linux 
 
The sizeof() operator returns the incorrect size of structures when they 
contain a mixture of integers and shorts or char types. 
 
sizeof() returns a 4 byte aligned size for the structure when the structure 
contains at least one integer and an odd number of shorts or chars 
 
ex1: 
struct a 
{ 
  int i; 
  short j; 
}; 
 
sizeof(struct a) is 8, it should be 6 
 
And it returns a 2 byte aligned size when a structure contains at least one 
short and and odd number of chars. 
 
Ex2: 
 
struct b 
{ 
  short i; 
  char j; 
}; 
 
sizeof( struct b ) is 4, it should be 3 
 
 
I understand why you might want 4 byte aligned values, but this has caused me 
mega bug headaches when writing binary files, since I end up more bytes than I 
intended and my opcode offsets get all fouled up. 
 
Here is some more code: 
 
struct 
try1 
{ 
  unsigned short a; 
}; 
 
struct 
try2 
{ 
  unsigned int   a; 
  unsigned short b; 
}; 
 
struct 
try3 
{ 
  unsigned int   a; 
  unsigned short b; 
  unsigned short c; 
}; 
 
struct 
try4 
{ 
  unsigned int   a; 
  unsigned short b; 
  unsigned short c; 
  unsigned short d; 
}; 
 
struct 
try5 
{ 
  unsigned int   a; 
  unsigned short b; 
  unsigned short c; 
  unsigned char  d; 
}; 
 
int 
main() 
{ 
  printf("Size %d my machine prints 2  should be 2\n", sizeof(struct try1)  ); 
  printf("Size %d my machine prints 8  should be 6\n", sizeof(struct try2)  ); 
  printf("Size %d my machine prints 8  should be 8\n", sizeof(struct try3)  ); 
  printf("Size %d my machine prints 12 should be 10\n", sizeof(struct try4) ); 
  printf("Size %d my machine prints 12 should be 9\n", sizeof(struct try5) ); 
}

-- 
           Summary: sizeof returns incorrect 4 byte aligned value when
                    structure contains integers and smaller types
           Product: gcc
           Version: 3.2.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: michaelcollins at ivorycity dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: gcc (GCC) 3.2.3 20030422
  GCC host triplet: (Gentoo Linux 1.4 3.2.3-r2, propolice)
GCC target triplet: x86


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]