Re: Alignment, Cast (more about structs...)



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"
.



Relevant Pages

  • Re: Alignment
    ... 2, double on multiple of 4, ints on even multiples of 8, pointers ... chars cannot require anything stricter than byte ... alignment for 'struct '. ...
    (comp.lang.c)
  • Re: problem with sizeof
    ... my compiler spits out a warning on the above ... Actually it is because they have different alignment requirements. ... aligned on a multiple of 1). ... the padding is located. ...
    (comp.lang.c)
  • Re: UDT memory alignment
    ... Probably the simplest way to look at this, is instead of calling it "dword ... the alignment is such that the whole ... positioned at an alignment that is a multiple of it's length, ... they can be placed without the need for preceding padding. ...
    (microsoft.public.vb.general.discussion)
  • Re: Fastcode Memory Manager Status 17
    ... > a bonus, there was a requirement of 16-byte alignment only for ... your effective block size is just larger than a multiple of 16 and you'll ... have to waste address space to enforce the alignment. ... marker before each block, then it would be easy to 16-byte align 12-byte ...
    (borland.public.delphi.language.basm)
  • Re: Alignment, Cast (more about structs...)
    ... alignment is the requirement that an object of a given type start ... at a multiple of some particular number of bytes. ... different reading methods are needed if the ECC bits ... the standard has sufficient room to allow all sorts of ...
    (comp.lang.c)