#include #include /* align_size has to be a power of two !! */ void *aligned_malloc(size_t size, size_t align_size) { char *ptr,*ptr2,*aligned_ptr; int align_mask = align_size - 1; ptr=(char *)malloc(size + align_size + sizeof(int)); if(ptr==NULL) return(NULL); ptr2 = ptr + sizeof(int); aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask)); ptr2 = aligned_ptr - sizeof(int); *((int *)ptr2)=(int)(aligned_ptr - ptr); return(aligned_ptr); } void aligned_free(void *ptr) { int *ptr2=(int *)ptr - 1; ptr -= *ptr2; free(ptr); } int main( void ) { // declare the type of data pointer that // we want to allocate... char* pData = 0; pData = aligned_malloc( 64, 4 ); // let's get 64 bytes aligned on a 4-byte boundry if( pData != NULL ) { ; // do something with our allocated memory // free it... aligned_free( pData ); pData = 0; } return 0; }