29925

Is there a portable way to know if uintptr_t is defined in stdint.h?

Question:

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 <a href="https://stackoverflow.com/questions/1845482/what-is-uintptr-t-data-type/1846648#1846648" rel="nofollow">it is guaranteed only in C, not in C++ (or C++11)</a>

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:

<ul><li>Is there a magic and guaranteed word that I can use where I put I_WONDER_IF_UINTPR_T_IS_DEFINED?</li> <li>Should I just use unsigned long and forget about it?</li> </ul>

Generated assembly (when uintptr_t available): <a href="http://goo.gl/4feUNK" rel="nofollow">http://goo.gl/4feUNK</a>

Note 1: I am aware than in C++11 I should use <a href="http://en.cppreference.com/w/cpp/language/alignof" rel="nofollow">alignof</a> in place of sizeof <br /> Note 2: I am aware of this discussion: <a href="https://stackoverflow.com/questions/13642827/cstdint-vs-stdint-h" rel="nofollow"><cstdint> vs <stdint.h></a>

Answer1:

If you really want to work with an implementation not providing uintptr_t when including <stdint.h>, consider using uintmax_t instead, and a static_assert for suitability (you already established you might be on a goofy setup, so it might be too small):

#include <stdint.h> static_assert(sizeof(uintmax_t) >= sizeof(void*), "need a bigger unsigned type."); // Add code using `uintmax_t` to round-trip data-pointers here

There are two dis-advantages though:

<ol><li>uintmax_t might not be uintptr_t, important for overload-resolution and linking.</li> <li>uintmax_t might be bigger than neccessary.</li> </ol>

Answer2:

In general, portable build systems on *nix tend to use the "autotools" approach which is to test-compile a trivial file containing the type in question, and if it compiles, you know you have that type. If you want something simpler, you could either (a) assume uintptr_t is available even in C++ (probably reasonable), or (b) use unsigned long long and then:

static_assert(sizeof(unsigned long long) >= sizeof(void*), "oops");

Answer3:

I know this is very old, but, how about using UINTPTR_MAX for your I_WONDER_IF_UINTPR_T_IS_DEFINED ?

Recommend

  • swapping “endianness” of floats and doubles
  • fseek on a position beyond EOF does not trigger EOF using feof, how come?
  • function template specialization syntax for cpp file
  • In C, can you have a 128-bit bitfield [duplicate]
  • Using stdint.h and ANSI printf?
  • Generate unique sequence ID
  • Is it possible to insert three numbers into 2 bytes variable?
  • What Causes Instruction Replay Overhead in CUDA
  • How to use make_transform_iterator() with counting_iterator and execution_policy in Thrust?
  • How to discover a file is changed in GIT during a push
  • OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS
  • Inserting NULL/empty string using libpqxx library
  • C: Custom strlen() library function
  • How to count amount of elements in a row of a matrix in C
  • Submission of new app with iAds
  • How to format data from string variable
  • How to work with AMMediaType for video filters
  • LIBRTMP Delphi: mapping of the DLL
  • Why does Sencha 2 only work in Webkit browsers?
  • How to unpack 32bit integer packed in a QByteArray?
  • XSLT foreach repeating nodes to flat
  • Does Apple allow the usage of sysctl.h within iOS applications?
  • Django invalid literal for int() with base 10
  • Sequential (transactional) API calls in angular 4 with state management
  • Appending Character to Character Array In C
  • xtable package: Skipping some rows in the output
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • C++ Partial template specialization - design simplification
  • Why is the size of this struct 32?
  • AES padding and writing the ciphertext to a disk file
  • Javascript Callbacks with Object constructor
  • Convert array of 8 bytes to signed long in C++
  • Matrix multiplication with MKL
  • Linker errors when using intrinsic function via function pointer
  • LevelDB C iterator
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?