Re: Alignment



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



Relevant Pages

  • 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)
  • RETURNing multiple values containing spaces?
    ... I was curious if there's some sort of trick invovled in returning multiple ... Bills "name2" is now a single string of hex chars, ... nor is using the data stack. ...
    (comp.lang.rexx)