Preamble: I want to convert a pointer to an integer type, e.g. to check alignment. uintptr_t
seems to be the correct type, but it is guaranteed only in C, not in C++ (or C++11)
For the following code:
#include <stdint.h>
#ifndef I_WONDER_IF_UINTPR_T_IS_DEFINED
typedef unsigned long uintptr_t;
#endif
template <typename T>
bool isAligned(unsigned char* p) ///Checks alignment with respect to some data type
{
return !((uintptr_t) p % sizeof(T));
}
template bool isAligned<uint16_t>(unsigned char* p);
template bool isAligned<uint32_t>(unsigned char* p);
template bool isAligned<uint64_t>(unsigned char* p);
2 questions:
- Is there a magic and guaranteed word that I can use where I put
I_WONDER_IF_UINTPR_T_IS_DEFINED
? - Should I just use
unsigned long
and forget about it?
Generated assembly (when uintptr_t available): http://goo.gl/4feUNK
Note 1: I am aware than in C++11 I should use alignof
in place of sizeof
Note 2: I am aware of this discussion: <cstdint> vs <stdint.h>