This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: structure size and structure element offset calculation issue
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Sivaprasad <sivaprasad dot pv at redpinesignals dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Fri, 23 May 2014 11:41:37 +0100
- Subject: Re: structure size and structure element offset calculation issue
- Authentication-results: sourceware.org; auth=none
- References: <5379DBEA dot 3050903 at redpinesignals dot com> <CAH6eHdSxUTYqD6w4eJaqbZxe59JGFrTuXbAEkvt3vZsaxq+9wQ at mail dot gmail dot com> <5379F909 dot 9010906 at redpinesignals dot com> <CAH6eHdQ1SyaXaQ997oXuBfjF30wigOqzbjmaDUxTqZAq33pHbA at mail dot gmail dot com> <537F1DA1 dot 7030407 at redpinesignals dot com>
On 23 May 2014 11:06, Sivaprasad wrote:
>
> I have a confusion about structure size calculation.
>
> I have tried to compared structure size result with
> gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) generates code for x86 machine
> and
> cross compiler with gcc version 4.3.4 generate code for custom processor.
>
> struct sample
> {
> short int s;
> }a;
>
> sizeof(a) showing 2 bytes with x86 gcc and 4 bytes with cross compiler.
>
> struct sample
> {
> int i;
> short int s;
> }a;
>
> sizeof(a) showing 8 bytes with x86 gcc and 8 bytes with cross compiler.
>
> When compiler will add hole at end of the structure ?
To ensure that if you have an array of those objects the int member
will always be on a 4-byte boundary.
> why cross compiler trying to make structure size multiple of 4 ?
Because GCC thinks short has an alignment of 4 on your custom processor.