tags:

views:

179

answers:

5

Hi Guys,

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

+3  A: 

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).

avakar
A: 

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++)

Philippe Leybaert
A: 

This might help you: http://lists.debian.org/debian-user/2006/04/msg00681.html. Unfortunatly your question seems to be compiler dependant.

Tobias Langner
+2  A: 

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.

I think your best bet is to use size_t. This is defined in the header <cstddef>. You may try using signed size_t, for example. long's tend to match the OS pointer size as well.

If you're looking for integers that do not change size based on the operating environment, take a look at the Boost Integer Library.

GMan
`signed size_t`? Perhaps you meant gcc's `ssize_t`? :)
avakar
+3  A: 

under Visual Studio you are also offered __int3264 which does much the same as INT_PTR ...

Goz