Re: Alignment in C99 for Program-Defined Allocation



On Aug 7, 8:53 am, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxxx> wrote:
On 8/7/2010 7:57 AM, Shao Miller wrote:

In C99, is it possible to write a well-defined program which
implements its own memory allocation in an alignment-responsible way,
without using the C99 memory management functions?

For example, out of some pool with 'static' storage duration?  I
cannot figure out how in any pool of 'unsigned char[XXX]', we could
determine the alignment for where a pointer might point to.

     For any object type T, the required alignment is a divisor
of sizeof(T).  In particular, alignment on a sizeof(T) boundary
is always tight enough, perhaps tighter than needed.

Right. 'sizeof (T)' cannot be a fractional multiple of the alignment
requirement for 'T', otherwise an array has misaligned elements.

     Unfortunately, that doesn't seem to help!  Since the family
of potential types in C is open-ended (in C99, even the family of
primitive types is open-ended), you can't just make a union of all
possible T and form your pool from an array of union instances.
Right.

Your malloc-equivalent could not be 100% sure that the memory
it returned was suitably aligned for all possible types; the caller
might be using a T you hadn't thought of.  You could do it for any
one program, perhaps, by cataloging every type the code uses, but
the maintenance headaches would be simply awful.

Right.

     The dodge of using a big char[] as the basis of the pool (a
theme you seem unhealthily attracted to)
It is attractive because: We can work with its elements without trap
representations. We can 'union' it to force alignment based on some
other types. If we have a non-portable notion of the representation
of some type of pointer as a raw byte address in a flat memory model
and address 0 being aligned for any type, that's all that's needed for
alignment, without any 'union'. See Christopher's post, for example.
We can copy objects as 'unsigned char[sizeof I]' ('I' is the
identifier designating an object.)

is flawed, the fundamental
problem being that there's no portable way to determine how the
array itself is aligned.
"Flawed" I'm not sure about. Determination of the array's alignment:
I don't know of a portable way, either.

 I think you just need to accept the fact
that some internals of memory management aren't portable.
It sure looks that way...
.



Relevant Pages

  • Re: Alignment in C99 for Program-Defined Allocation
    ... without using the C99 memory management functions? ... out of some pool with 'static' storage duration? ... determine the alignment for where a pointer might point to. ... that some internals of memory management aren't portable. ...
    (comp.lang.c)
  • Re: Question regarding memory alignment
    ... In K&R2 page number 186 one union is used to enforce the alignment as ... The standard requires that a pointer to member of a union, when converted to a pointer to the union type, must compare equal to a pointer to the union itself, and vice versa. ... Therefore, it's alignment requirement is likely to be the least common multiple of the alignment requirement of each of it's members, and it is guaranteed to be a common multiple, even if it isn't the least one. ...
    (comp.lang.c)
  • Question regarding memory alignment
    ... In K&R2 page number 186 one union is used to enforce the alignment as ... union header *ptr; ... I am not able to understand how Align field forces the alignment? ...
    (comp.lang.c)
  • Re: Question regarding memory alignment
    ... This section of the book is not talking about the standard malloc ... In K&R2 page number 186 one union is used to enforce the alignment as ... union header *ptr; ...
    (comp.lang.c)
  • Re: Alignment
    ... Given that any object type can be contained in a structure or a union, ... all structure and union types have the same alignment requirements, ... other pointer types is very likely ...
    (comp.lang.c)