Reputation: 547
What is the compatible "int" datatype in C++ that can resize itself to 4 bytes on 32bit & 8 bytes on 64bit windows?
Although INT_PTR works fine but it reduces the readability as well as its description tells us to use it for pointer arithmetic.
Thanks
Upvotes: 6
Views: 751
Reputation: 504313
The standard does not mention specific size requirements, only that each integral type must provide at least as much storage as the type before it. So int
must hold as much as a short
, and so on. You're better off specifying what you need them for.
If you're looking for integers that do not change size based on the operating environment, take a look at the Boost Integer Library, or the C99/C++11 header <cstdint>
. This contains types such as uint32_t
/int32_t
and uintmax_t
/intmax_t
.
Most importantly, based off your question, it has: uintptr_t
/intptr_t
. These are guaranteed to have the correct size to hold a pointer on your platform.
Upvotes: 3
Reputation: 62333
under Visual Studio you are also offered __int3264 which does much the same as INT_PTR ...
Upvotes: 3
Reputation: 10828
This might help you: http://lists.debian.org/debian-user/2006/04/msg00681.html. Unfortunatly your question seems to be compiler dependant.
Upvotes: 0
Reputation: 171914
It really depends on the compiler. I think the only (more or less) reliable way is by using a pointer type like (void *).
I think the best way is by using some conditional processing in your header file and set a custom type:
#ifdef _WIN64
typedef __int64 NATIVEINT;
#else
typedef __int32 NATIVEINT;
#endif
(this sample is for Visual C++)
Upvotes: 1
Reputation: 32685
If you're looking for something standard, you're out of luck. The standard does not specify the size of any of the built-in datatypes.
Note, that INT_PTR
does not imply pointer arithmetic. I means that the type will have the same size as void *
, which is exactly what you want. It won't work on all platforms though (I'm pretty sure it's Windows specific).
Upvotes: 3