Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Alignment requirement of basic types |
| Date | 2015-04-03 16:46 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <20150403092630.239@kylheku.com> (permalink) |
| References | <mfm5mg$4rp$1@dont-email.me> |
On 2015-04-03, Noob <root@127.0.0.1> wrote:
> Hello,
>
> 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.
The C standards allows implementations to impose alignment
requirements on pointers to types that are more than one
byte wide.
> I suppose I was mistaken?
>
> Do you know platforms where 4-byte types are only 2-byte aligned?
On x86, four byte types do not have an alignment *requirement*.
This means that you can do this:
char *mem = malloc(5);
int *p = (int *) mem + 1;
*p = 42;
since the alignment requirement is not imposed, you have more
freedom in how you treat this memory. Of course, the code is not
portable: strictly portable ISO C code has to assume that the
alignment of int is the strictest it can be: sizeof (int).
Now in spite of there not being an alignment requirement from the hardware, x86
compilers do align four byte integers, when they control their allocation: e.g.
as static or local variables, or struct members.
This is not required; it is for reasons of performance or
layout compatibility.
ISO C does not dictate all the details about how declared objects,
and structure members should be laid out.
There are some rules. A struct must include any necessary padding so that all
its members are properly aligned with regard to their alignment requirements,
when that struct is aggregated into an array. That padding must be at the
end of the structure. Members are laid out in a stucture at increasing
addreseses in the order in which their declarations appear.
> Perhaps 8086 had 32-bit longs that were only 16-bit aligned?
The 8086 and 8088 processors do not have alignment restrictions; you can read
words from odd addresses. Many classic CISC processors are that way, such
as Motorola 68K.
Some processors with a RISC-y lineage enforce alignment, but have provisions
for trap handlers in the operating system kernel to emulate misaligned
accesses (very expensively). So this code will work:
char *mem = malloc(5);
int *p = (int *) mem + 1;
*p = 42;
but the *p = 42 will trigger a trap that could cost hundreds of machine
cycles to execute.
On a processor which supports misaligned accesses in hardware, thre
could still be a performance penalty, though it won't be hundreds
of cycles. More bus cycles may be needed for the hardware to access
two adjacent aligned words.
Some RISC-like instruction sets also have special instructions for "skewed"
memory accesses, which read or write a word-sized quantity at an aligned
address, but also shift it at the same time. With a suitable pair of these
instructions, you can read a 32 bit value (or whatever) at an address that is 1
mod 4 (or whatever).
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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