Okay, I should be doing other things but couldn't resist having a play,
this time with an example from code closer to what exists in many places in the app code..
With
ICC on Win32 , Methods #1, #2, & #3 generate warnings , Method #4 union cast doesn't. (Haven't tried straight MS yet).
Still in all cases 'seem' much more unwieldy than the original (#1) elegant line, Recombination and/or use of macro(s) may improve that.
@Joe: Not sure, but I hope Method 2 captured 'spirit' of the uintptr_t typecast, depends if you rather intended &src to be a buffer pointer, or buffer pointer pointer (if that makes any difference in casting I don't know:D, can always tweak another method #5
Jason
//---------------------------------------------------------------------
//METHOD 1: typical code
// Original code Generates
warning #1684: conversion from pointer to
// same-sized integral type (potential portability problem)
//
// defined elsewhere : 'typedef size_t PointerArith' and 'typedef unsigned int size_t'
palignedMem = ( void * ) ( ((PointerArith) pmem + byteAlignment + sizeof(void *)) &~(byteAlignment) );
//---------------------------------------------------------------------
//METHOD 2: uintptr_t integral type casts
//adapted attempt from Joe's suggestion for different example: (uintptr_t) &src & (uintptr_t)15
// Arithmetic seperated out for analysis
// Additionally defined elsewhere: #define PTR_SIZE sizeof(void *)
uintptr_t uipt = (uintptr_t) pmem; // This line makes the
warning uipt += (byteAlignment + PTR_SIZE);
uipt &= ~byteAlignment;
palignedMem = (void *) uipt;
//---------------------------------------------------------------------
//METHOD 3: similar integral type casts
//Another try slightly different (Arithmetic also broken up for analysis)
UINT_PTR uipMem = (UINT_PTR) pmem; // This line generates the
same warning uipMem += (byteAlignment + PTR_SIZE);
uipMem &= ~byteAlignment;
palignedMem = (void *) uipMem;
//---------------------------------------------------------------------
//METHOD 4: Trying out the union cast -
no warnings!// the typedef PtrCheck union { ...i, ...p } can be any platform specific one:
// where i is of integral type same size as pointer p
ptrCheck ptrcheckMem;
ptrcheckMem.p = (void *) pmem;
ptrcheckMem.i += (byteAlignment + PTR_SIZE);
ptrcheckMem.i &= ~byteAlignment;
palignedMem = (void *) ptrcheckMem.p;
//---------------------------------------------------------------------