View Full Version : VAR pointer alignment
05-03-2001, 05:05 AM
I'm too tired to think about this - what's an easy way to align to a 4-byte boundary?
When you alloc the ptr, it should come to you as 4byte aligned. Then as long as you use it/alloc it in 4bytes chunks, it will always be aligned.
However, to arbitrarily align it do this:
ptr = (type_ptr*)(( ((unsigned char*)ptr) + 3)& ~3)
Some macros you find useful for general number alignment are
#define M_Align2( _a ) ( ((_a)+1) & (~1) )
#define M_Align4( _a ) ( ((_a)+3) & (~3) )
#define M_Align8( _a ) ( ((_a)+7) & (~7) )
#define M_Align16( _a ) ( ((_a)+15) & (~15) )
But if you are doig Alignment with ptrs, maker certain to cast them to unsigned_char_ptrs first, or it will align them to the size of the ptr, not bytes!
Or maybe just create a pointer to a long or to a float, which are guaranteed to be on a 4 byte boundary. Then just cast it to a void pointer, or whatever kind of pointer you want.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.