c/6385: sizeof fails calculating size of an structure (sizeof(struct my_struct))

gad@reymad.com gad@reymad.com
Sat Apr 20 07:13:00 GMT 2002


>Number:         6385
>Category:       c
>Synopsis:       sizeof fails calculating size of an structure (sizeof(struct my_struct))
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Sat Apr 20 06:46:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Gonzalo Aguilar : gad@reymad.com
>Release:        gcc-3.0.4
>Organization:
>Environment:
linux 2.4.17 glibc 2.2.3
>Description:
This structure:

struct msh_header
{
  char charid[3];
  int objs;
};

is evaluated as a 8 bytes size object.

It sould be 7 bytes. 

It seems that aligns data to a 4 byte boundary but this is not what should happen cause structure MUST be 7.

Even this will be 8 bytes

struct msh_header
{
  char charid[1];
  int objs;
};


And this 12 bytes size:

struct msh_header
{
  char charid[5];
  int objs;
};

>How-To-Repeat:
Just compile this code 

#include <stdio.h>

struct msh_header
{
  char charid[3];
  int objs;
};

int main(int argc, char **argv)
{
   printf("Size %i should be %i",sizeof(struct msh_header),7);

return(1);

}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list