Re: [GD-Windows] using typedef to create aligned types
Brought to you by:
vexxed72
From: Javier A. <ja...@py...> - 2004-12-09 15:24:14
|
The compiler should call operator new with an adjusted size to cope with both the size of the objects and the padding needed to guarantee alignment, and then perform alignment of the pointer. Off the top of my head, something like this: EffectiveSize = (DataSize + Alignment -1) & ~(Alignment-1) SizeNeeded = EffectiveSize * ArraySize AlignedSizeNeed = SizeNeeded + (Alignment-1) Pointer = new(AlignedSizeNeeded) AlignedPointer = (Pointer + Alignment-1) & ~(Alignment-1) Call the constructor for AlignedPointer and afterwards in EffectiveSize increments I'm betting the compiler calculates the correct numbers, and inserts the final alignment code after the call to operator new. The call to delete[] will need the original Pointer value, so I would imagine the physical pointer to an aligned type is either bigger (to hold both the returned Pointer and the usable AlignedPointer), or the compiler keeps Pointer but does the conversion every time it is used. Also my understanding is that sizeof(*p) = (char*)(p+1) - (char*)(p) by definition, so a class with an int is sizeof 4 and a class with an int and an alignment of 16 must be sizeof 16. Save typos, that should give an idea of what's going on. -- Javier Arevalo Pyro Studios ----- Original Message ----- From: Daniel Glastonbury int s1 = sizeof(Fred); int s2 = sizeof(FredX); s1 is 4, s2 is 16 His explanation was to do with operator new[] failing if it didn't adjust the size. But my question is then, how does operator new cope with the typedef version? Incorrectly? |