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 16:23:35 +0530	[thread overview]
Message-ID: <030b01c599ab$fb845be0$9900a8c0@ispl091> (raw)
In-Reply-To: 6a00c8d50508050315501d900c@mail.gmail.com

This should not happen until compiler option /Zp is explicitly set to 8.
Quoted from msdn
" ANSI 3.5.2.1   The padding and alignment of members of structures and
whether a bit field can straddle a storage-unit boundary
Structure members are stored sequentially in the order in which they are
declared: the first member has the lowest memory address and the last member
the highest.
Every data object has an alignment-requirement. The alignment-requirement
for all data except structures, unions, and arrays is either the size of the
object or the current packing size (specified with either /Zp or the pack
pragma, whichever is less). For structures, unions, and arrays, the
alignment-requirement is the largest alignment-requirement of its members.
Every object is allocated an offset so that
offset % alignment-requirement == 0
Adjacent bit fields are packed into the same 1-, 2-, or 4-byte allocation
unit if the integral types are the same size and if the next bit field fits
into the current allocation unit without crossing the boundary imposed by
the common alignment requirements of the bit fields. "
Source Url:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/impdf_34.asp

Regards,
Amit Dang
----- Original Message ----- 
From: "Steve Graegert" <graegerts@gmail.com>
To: "Glynn Clements" <glynn@gclements.plus.com>
Cc: "Amit Dang" <amit_dang@intersolutions.stpn.soft.net>;
"linux-c-programming" <linux-c-programming@vger.kernel.org>
Sent: Friday, August 05, 2005 3:45 PM
Subject: Re: Any pointer to Byte Alignment & Structure Padding?


> On 8/5/05, Glynn Clements <glynn@gclements.plus.com> wrote:
> >
> > Steve Graegert wrote:
> >
> > > >    Thanks for your prompt response and the valuable information. But
I
> > > > tried following on Linux 32-bit gcc 2.96
> > > > struct temp {
> > > >    long long i;
> > > >    char c;
> > > > } and sizeof(struct temp) gave 12 not 16.
> > >
> > > Hmm, I have no access to a 32-bit machine right now, but I am running
> > > GCC 3.4.2 on SPARC and sizeof(temp) gives 16 as expected.
> >
> > long long is likely to be 8-byte aligned on 64-bit systems and 4-byte
> > aligned on 32-bit systems.
>
> Agree.  Interestingly, Microsofts VCC (was quite curious, so I did a
> quick test) aligns long long on an 8-byte boundary resulting in a size
> of 16 for the structure given above. (Yes it's a 32-bit machine.)
> Don't know the reason for that, since the ABI for i386 does not
> mention this case explicitly.
>
> > > One thing a could think of is that on 32-bit machines it makes no
> > > sense to pad to 16 bytes since the natural word size (size of internal
> > > registers) is 4 bytes resulting in an unnecessary read operation to
> > > fetch the rest of the structure that is useless anyway.
> >
> > Aligment is determined by the granularity of RAM access. E.g. 32-bit
> > systems tend to have RAM organised as 32-bit words; reading a 32-bit
> > value which issn't aligned requires reading two adjacent words,
> > shifting and OR-ing them together to obtain the desired value.
>
> Yes, sure, but why would Microsoft's C compiler generate code that
> results in a fully despensable read operation for the padded word?  (I
> know this is a Linux programming list, but the question seems related
> to the overall topic of data alignment in memory.)
>
> Regards
>
> \Steve
>
> --
>
> Steve Graegert <graegerts@gmail.com>
> Software Consultancy {C/C++ && Java && .NET}
> Mobile: +49 (176)  21248869
> Office: +49 (9131) 7126409
> -
> 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


  reply	other threads:[~2005-08-05 10:53 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 [this message]
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='030b01c599ab$fb845be0$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).