Re: Alignment
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 20 Aug 2007 16:27:11 -0700
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Keith Thompson wrote:[...]
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Even all this is suspect. Nothing forbids saying, for example,
that chars must be aligned on odd addresses, short on multiple of
2, double on multiple of 4, ints on even multiples of 8, pointers
on multiples of 3. Granted the result is peculiar. It is up to
the implementor to satisfy all these for malloc/realloc.
Not quite. chars cannot require anything stricter than byte
alignment. Furthermore, your scheme would not allow any legal
alignment for 'struct { char c; short s; }'.
The alignment of any type must be a factor of its size. It's not
required that all alignments are multiples or factors of each other,
but I've never heard of an implementation where they aren't. For
example, if int requires 3-byte alignment and float requires 4-byte
alignment, then a pointer returned by malloc() must point to a chunk
of memory that's at least 12-byte aligned.
I am not pushing "my scheme", just pointing out that the proposed
means of discovering alignment requirements are not necessarily
viable.
I didn't mean to imply that you're pushing what I called your "scheme"
(I think "hypothetical implementation" would have been a better choice
of words), and I agree with your conclusion. I was just pointing out
a conforming implementation cannot require odd addresses for chars.
The rest of your hypothetical requirements would be perfectly ok, as
long as each type's size is a multiple of its alignment (arrays can't
have gaps).
It might have been useful for the standard to define a macro
ALIGN_MAX, specifying the maximum alignment of any type (typically 4
or 8), and perhaps a typedef for some arbitrary type that requires
that alignment. Most code wouldn't need it, but it would make it
possible to write a portable malloc-like function.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- Alignment
- From: fishpond
- Re: Alignment
- From: Ark Khasin
- Re: Alignment
- From: Eric Sosman
- Re: Alignment
- From: Ark Khasin
- Re: Alignment
- From: CBFalconer
- Re: Alignment
- From: Keith Thompson
- Re: Alignment
- From: CBFalconer
- Alignment
- Prev by Date: Re: Malcolm's new book - Chapter 1 review
- Next by Date: Re: parsing a text line vertically
- Previous by thread: Re: Alignment
- Next by thread: Re: Alignment
- Index(es):
Relevant Pages
|