Re: using typedef to create aligned types

"Javier Arevalo" <[email protected]> Thu, 9 Dec 2004 16:28:32 +0100
Newsgroups gmane.games.devel.windows
Message-ID <[email protected]>
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?



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555