Re: Alignment, Cast (more about structs...)
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 31 Aug 2007 01:53:14 -0700
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Keith Thompson wrote:
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Richard Tobin wrote:
Richard Bos <rlb@xxxxxxxxxxxxxxxxxxxxxx> wrote:
And there could even be - but probably are not right now -
machines where it must start at an odd address.
But the C standard does not allow for this: its concept of
alignment is the requirement that an object of a given type start
at a multiple of some particular number of bytes.
Chapter and verse please. I think you can come to this conclusion
only by making some earlier assumptions about the memory system.
AFAIK the only stricture is that malloc returns an address aligned
for ALL types.
C99 3.2:
alignment
requirement that objects of a particular type be located on
storage boundaries with addresses that are particular multiples of
a byte address
As has been discussed here, this glosses over just what it means for
an address to be a "multiple" of a byte address, but it seems to allow
for alignments like 2N while excluding alignments like 2N+1.
That only covers alignment for one particular type. For something
like malloc you need different rules.
For malloc, you just need an alignment that's consistent with the
alignment of all types, at least the least common multiple of all
alignments.
Imagine a system with 9 bit
chars, such that:
sizeof is
====== ==
short 2
int 3
long 4
float 4
double 6
which would require a universal alignment multiple of 12. Note
that I have not specified the maximums for those various types. As
described above there is room for at least two trapping bits,
allowing detection of uninitialized use, etc.
You've only specified the sizes. Are you assuming that each type must
be aligned to its size (e.g., double requires 6-byte alignment)?
In practice actual memory systems have their own width, which needs
to be sufficient to allow for ECC coding for all items other than
bytes. This expands an 8 octet unit to be at least 72 bits. Two
different reading (and writing) methods are needed if the ECC bits
are to be visible to the system. In this case the system MIGHT
look like:
sizeof is live binary bits
====== == ================
char 1 9
short 3 24
int 4 32
long 6 48
long long 8 64
float 4 whatever
double 8 whatever
and the alignment multiple for malloc needs to be 12 again.
Why would you want ECC bits to be visible to C software? You can't
meaningfully set them or read them, can you?
But ok, I suppose they could be treated as padding bits, so short has
a width of 24 bits plus 3 padding bits. But alignments are in terms
of bytes, so I don't see how padding bits are relevant. (The
standard's concept of padding bits doesn't apply to floating-point
types, but they can still have bits that don't contribute to the
value.)
At any rate, there is room to play about, and possibly arrange for
a truly universal international char. set. If I have anything to
say about it, such a set will have made adequate allowance for ECC
checking.
At any rate, the standard has sufficient room to allow all sorts of
memory arrangements with various practical advantages.
Not *all* sorts. In particular, it doesn't allow an implementation to
require a 2-byte type (say short) to be allocated at an odd address,
since it would violate the definition of "alignment". That was the
point I was making above; I don't believe you addressed it.
--
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"
.
- Follow-Ups:
- Re: Alignment, Cast (more about structs...)
- From: CBFalconer
- Re: Alignment, Cast (more about structs...)
- References:
- Alignment, Cast
- From: anon . asdf
- Re: Alignment, Cast (more about structs...)
- From: anon . asdf
- Re: Alignment, Cast (more about structs...)
- From: Eric Sosman
- Re: Alignment, Cast (more about structs...)
- From: Richard Bos
- Re: Alignment, Cast (more about structs...)
- From: Richard Tobin
- Re: Alignment, Cast (more about structs...)
- From: CBFalconer
- Re: Alignment, Cast (more about structs...)
- From: Keith Thompson
- Re: Alignment, Cast (more about structs...)
- From: CBFalconer
- Alignment, Cast
- Prev by Date: Re: Problem With Pro*c
- Next by Date: Re: How to force fscanf to find only data on a single input line?
- Previous by thread: Re: Alignment, Cast (more about structs...)
- Next by thread: Re: Alignment, Cast (more about structs...)
- Index(es):
Relevant Pages
|