All of lore.kernel.org
 help / color / mirror / Atom feed
From: wwp <subscript@free.fr>
To: linux-c-programming@vger.kernel.org
Subject: Re: Any pointer to Byte Alignment & Structure Padding?
Date: Mon, 1 Aug 2005 16:11:25 +0200	[thread overview]
Message-ID: <20050801161125.716db5ff@localhost.localdomain> (raw)
In-Reply-To: <015a01c59694$80662070$9900a8c0@ispl091>

Hello Amit,


On Mon, 1 Aug 2005 17:57:48 +0530 "Amit Dang"
<amit_dang@intersolutions.stpn.soft.net> wrote:

> Hi Vadiraj,
>     Thanks for the explaination but when i try following structure
> struct temp
>  {
>         char c;   /* 1 byte lenght */
>         int i;      /* 4 byte length */
>         char c1; /* 1 byte length */
>         long long d /* 8 bytes  lenght */
>  };
> on a linux machine x86 32-bit with gcc 2.96. It gives its size = 20 bytes
> not 24 bytes (as explained by you)

See below (this applies to both members sorting examples from Vadiraj):

struct           padded  offset
member     size  size    range
-------------------------------
c          1     4       0-3
i          4     4       4-7
c1         1     4       8-11
d          8     8       12-19

So, 20 bytes. Isn't it right?


Regards,

> ----- Original Message ----- 
> From: "Vadiraj" <vadiraj.cs@gmail.com>
> To: "Amit Dang" <amit_dang@intersolutions.stpn.soft.net>
> Sent: Monday, August 01, 2005 5:44 PM
> Subject: Re: Any pointer to Byte Alignment & Structure Padding?
> 
> 
> > On 8/1/05, Amit Dang <amit_dang@intersolutions.stpn.soft.net> wrote:
> > > Hi,
> > >    Can any body provide some light on Byte Alignment & Structure Padding
> > > for gcc linux x86 32-bit?
> >
> >  The system expects the address of a variable to be multiple of
> > its size. Meaning for 32 bit x86 int being 4 bytes. The address
> > location of a int variable is expected to be at multiple of 4.
> > ex 0 4 8 12 16. if  its double then its expected it to be multiple of 8.
> > 0 8 16 ...
> >
> >  In case of structure allignment... this is achieved by padding.
> > if this is the structure
> > struct temp
> > {
> >        char c;   /* 1 byte lenght */
> >        int i;      /* 4 byte length */
> >        char c1; /* 1 byte length */
> >        long long d /* 8 bytes  lenght */
> > };
> >
> >  c starts at offset x( x is assured 4 byte alligned by gcc), i should
> > start at x+4 as it has to be multiple of 4 3 bytes of padding will be
> > done by gcc.
> > c1 starts at x+9, no padding is required char is 1 byte.
> > d starts at x+16,7 bytes of padding to get multiple of 8.
> >
> > It would differ if you re arrange the struct like this.
> > struct temp
> > {
> >        char c;   /* 1 byte lenght */
> >        int i;      /* 4 byte length */
> >        long long d /* 8 bytes  lenght */
> >        char c1;
> > };
> >
> > for same base offset...i will be from x+4 d would start from x+8,
> > there would be no padding for d  and c1 at x+16.
> >
> > I hope it helps.
> > -- 
> > cheers,
> > Vadi
> 
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


-- 
wwp


  reply	other threads:[~2005-08-01 14:11 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-01 11:43 Any pointer to Byte Alignment & Structure Padding? Amit Dang
     [not found] ` <6eee1c40508010514517b5b90@mail.gmail.com>
2005-08-01 12:16   ` Vadiraj
     [not found]     ` <17134.43470.280296.644313@cerise.gclements.plus.com>
2005-08-02 11:21       ` Vadiraj
2005-08-02 11:40         ` Amit Dang
2005-08-02 16:39           ` Glynn Clements
2005-08-02 17:12             ` Vadiraj
2005-08-03  4:04               ` Amit Dang
     [not found]     ` <673ac06405080402432d0feda3@mail.gmail.com>
2005-08-04 15:23       ` Vadiraj
2005-08-04 16:28         ` Steve Graegert
2005-08-05 16:51           ` Vadiraj
2005-08-05  4:14         ` Amit Dang
2005-08-05  6:32           ` Steve Graegert
2005-08-05  6:49             ` Amit Dang
2005-08-05  7:09               ` Steve Graegert
2005-08-05 11:19                 ` Glynn Clements
2005-08-05 10:15                   ` Steve Graegert
2005-08-05 10:53                     ` Amit Dang
2005-08-05 11:13                       ` Steve Graegert
2005-08-05 11:28                         ` Amit Dang
2005-08-05 11:37                           ` Steve Graegert
2005-08-05 12:59                     ` Glynn Clements
2005-08-05 11:31                       ` Steve Graegert
2005-08-01 12:27   ` Amit Dang
2005-08-01 14:11     ` wwp [this message]
     [not found]       ` <003b01c59715$d3ffef00$9900a8c0@ispl091>
     [not found]         ` <6a00c8d5050801234267dd0f7f@mail.gmail.com>
     [not found]           ` <01dd01c5972f$5c3b62a0$9900a8c0@ispl091>
     [not found]             ` <17135.20687.821822.269575@cerise.gclements.plus.com>
     [not found]               ` <02c401c59745$2aaa0410$9900a8c0@ispl091>
2005-08-02 11:08                 ` Progga
2005-08-02 11:34                   ` Amit Dang
2005-08-02 11:32                 ` Steve Graegert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050801161125.716db5ff@localhost.localdomain \
    --to=subscript@free.fr \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.