Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #59520

Re: Alignment requirement of basic types

From Tim Prince <tprince@computer.org>
Newsgroups comp.lang.c
Subject Re: Alignment requirement of basic types
Date 2015-04-03 10:23 -0400
Message-ID <co7m3gF294fU1@mid.individual.net> (permalink)
References <mfm5mg$4rp$1@dont-email.me>

Show all headers | View raw


On 4/3/2015 9:48 AM, Noob wrote:

> I had always been under the impression that objects of a given type
> had to be size-aligned, i.e.
> 2-byte objects had to be 2-byte aligned,
> 4-byte objects had to be 4-byte aligned,
> etc
> 
> However, I could not find this guarantee in C99 TC3 (N1256).
> And another poster claims that platforms may allow 4-byte
> objects to be {1,2,4}-byte aligned.
> 
> I suppose I was mistaken?
> 
> Do you know platforms where 4-byte types are only 2-byte aligned?
> 
> Perhaps 8086 had 32-bit longs that were only 16-bit aligned?
> 
> Maybe recent platforms have 64-bit long longs that are only
> 32-bit aligned?

The "guarantee" for mis-aligned data e.g. in a struct is that padding
will vary among platforms.  On the most common platforms it can be a
trade-off between speed and compactness of data structures.
The x86 32-bit mode platforms have no inherent 64-bit alignment for
int64_t scalar data types, as they are implemented as 32-bit data pairs,
while the 64-bit mode on the same CPUs includes native 64-bit int64_t
with preferred alignment.  Yet both modes have alignment requirements
for vectorized int64_t.  In AVX mode the alignment requirement for
32-byte objects increases to 32-byte alignment.
As far as I know, the alignment requirements for objects bigger than 8
bytes or 64 bits are not in the C standard, but are part of OS ABI.
As you allude, there is support for odd byte alignment of scalar data
types on this hardware, at a penalty in performance.
So you can waste a lot of time and incur non-portability by exploiting
the ability of these platforms to support non-standard alignments.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Alignment requirement of basic types Noob <root@127.0.0.1> - 2015-04-03 15:48 +0200
  Re: Alignment requirement of basic types Tim Prince <tprince@computer.org> - 2015-04-03 10:23 -0400
  Re: Alignment requirement of basic types James Kuyper <jameskuyper@verizon.net> - 2015-04-03 11:37 -0400
    Re: Alignment requirement of basic types John Temples <usenet@xargs-spam.com> - 2015-04-03 17:21 +0000
      Re: Alignment requirement of basic types James Kuyper <jameskuyper@verizon.net> - 2015-04-03 13:45 -0400
        Re: Alignment requirement of basic types Kaz Kylheku <kaz@kylheku.com> - 2015-04-03 18:40 +0000
      Re: Alignment requirement of basic types Keith Thompson <kst-u@mib.org> - 2015-04-03 10:50 -0700
      Re: Alignment requirement of basic types glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-03 19:02 +0000
    Re: Alignment requirement of basic types Kaz Kylheku <kaz@kylheku.com> - 2015-04-03 18:01 +0000
  Re: Alignment requirement of basic types Richard Damon <Richard@Damon-Family.org> - 2015-04-03 12:05 -0400
    Re: Alignment requirement of basic types glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-03 16:48 +0000
  Re: Alignment requirement of basic types Kaz Kylheku <kaz@kylheku.com> - 2015-04-03 16:46 +0000
    Re: Alignment requirement of basic types Keith Thompson <kst-u@mib.org> - 2015-04-03 11:06 -0700
      Re: Alignment requirement of basic types Robert Wessel <robertwessel2@yahoo.com> - 2015-04-05 22:22 -0500
  Re: Alignment requirement of basic types Waldek Hebisch <hebisch@antispam.uni.wroc.pl> - 2015-04-04 23:15 +0000

csiph-web