Re: Alignment in C99 for Program-Defined Allocation
- From: Shao Miller <sha0.miller@xxxxxxxxx>
- Date: Sat, 7 Aug 2010 10:36:54 -0700 (PDT)
On Aug 7, 8:53 am, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxxx> wrote:
On 8/7/2010 7:57 AM, Shao Miller wrote:Right. 'sizeof (T)' cannot be a fractional multiple of the alignment
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.
requirement for 'T', otherwise an array has misaligned elements.
Unfortunately, that doesn't seem to help! Since the familyRight.
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.
Your malloc-equivalent could not be 100% sure that the memoryRight.
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.
The dodge of using a big char[] as the basis of the pool (aIt is attractive because: We can work with its elements without trap
theme you seem unhealthily attracted to)
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"Flawed" I'm not sure about. Determination of the array's alignment:
problem being that there's no portable way to determine how the
array itself is aligned.
I don't know of a portable way, either.
I think you just need to accept the factIt sure looks that way...
that some internals of memory management aren't portable.
.
- References:
- Alignment in C99 for Program-Defined Allocation
- From: Shao Miller
- Re: Alignment in C99 for Program-Defined Allocation
- From: Eric Sosman
- Alignment in C99 for Program-Defined Allocation
- Prev by Date: print alphabet
- Next by Date: Re: "claim", etc. (was Re: C Standard Regarding Null Pointer Dereferencing)
- Previous by thread: Re: Alignment in C99 for Program-Defined Allocation
- Next by thread: Re: Alignment in C99 for Program-Defined Allocation
- Index(es):
Relevant Pages
|