linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Amit Dang" <amit_dang@intersolutions.stpn.soft.net>
To: linux-c-programming <linux-c-programming@vger.kernel.org>
Subject: Re: Any pointer to Byte Alignment & Structure Padding?
Date: Fri, 5 Aug 2005 09:44:16 +0530	[thread overview]
Message-ID: <00c001c59974$32b0c9b0$9900a8c0@ispl091> (raw)
In-Reply-To: 6eee1c40508040823165f1df7@mail.gmail.com

Hi Vadiraj,
The statement " Take it for granted you get either 4 byte or 8 byte boundary
but never 1 byte." you made is it generic or just valid for the structure in
question? If its generic then I have a question.
Why the size of
struct {
    char i;
    char j;
    char k;
} is 3 ? (gcc 2.96 on Linux 32-bit machine).
What I have understood atleast for gcc compiler Linux 32-bit machine is
that, Maximum byte boundary is 4. Structure will be aligned to byte boundary
equal to minimum of (4 or field with maximum size (within the structure)).
i.e. for the above example maximum field size if 1 and min (4, 1) = 1, so
structure is aligned to 1 byte.
If I have following structure
struct {
    short i;
    char j;
} its size will be 4.

if i modify the above struct to
struct {
    int i;
    char j;
} its size will be 8.
Now modifying int to long long in the above structure will have a size of
12 not 16 because byte alignment min (4, 8) = 4.

Assumptions: sizeof(char) = 1 byte, sizeof(short) = 2 byte, sizeof(int) = 4
byte, sizeof(long long) = 8 bytes.
Valid for gcc 2.96 Linux 32-bit machine

Regards,
Amit Dang
Regards,
Amit Dang


----- Original Message ----- 
From: "Vadiraj" <vadiraj.cs@gmail.com>
To: "Prabhat Hegde" <pubs.hegde@gmail.com>
Cc: "linux-c-programming" <linux-c-programming@vger.kernel.org>
Sent: Thursday, August 04, 2005 8:53 PM
Subject: Re: Any pointer to Byte Alignment & Structure Padding?


> On 8/4/05, Prabhat Hegde <pubs.hegde@gmail.com> wrote:
> > According to what you have said, dont you think it depnds from which
> > address your affset begins.
> >   Considring your last example,
> >    struct temp
> > > {
> > >       char c;   /* 1 byte lenght */
> > >       int i;      /* 4 byte length */
> > >       long long d /* 8 bytes  lenght */
> > >       char c1;
> > > };
> >
> >   Since char (1 byte ) can start anywhere withour padding,
> >   Lets take x to be at address X wherein (X mod 4 = 3), then in that
> > case "i" will have no padding since char (1 byte ) will make the
> > starting address of the int to agree with a 4 byte boundary.
> >   Now again the next long having a 8 byte boundary willl be/ may not
> > be padded depanding on the starting address.
> >  So what i feel is that begin the variable which has the maximum byte
> > boundary requirement. Now since all nos divisible by 8 are divisible
> > by 4, we will be cutting out the padding for int. Char can come in the
> > last.
>
>   The starting address is assured to be 4 byte boundary. Some
> compilers provide 8 byte boundary too. They never start with 1 byte
> boundary.
>
> That is the reason why the structure gets padded even at the end. To make
it
> 4 bytes alligned.
>
>
> >   hence what if the structure is like the below:
> >    struct temp{
> >        long lond d;   // x + padding if required to suffice 8 byte
boundary
> >        int i;             // no padding
> >        char c;         // no padding
> >        char c1;       // no padding
> >        };
>
>   For this example the size should come to 8+4+1+1=14 bytes. But when
> you check it will be 16 bytes. Due to padding, to make it 4/8 byte
> boundry.
>
> Take it for granted you get either 4 byte or 8 byte boundry but never 1
byte.
>
> Arranging the structure as mentioned above will definitely remove
> portablility issue
> as for both 4 byte or 8 byte aligned compilers the size will be 16 bytes.
>
> -- 
> 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


  parent reply	other threads:[~2005-08-05  4:14 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 [this message]
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
     [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='00c001c59974$32b0c9b0$9900a8c0@ispl091' \
    --to=amit_dang@intersolutions.stpn.soft.net \
    --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 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 NNTP newsgroup(s).