Hao Shi Hao Shi - 1 year ago 131
C++ Question

c++ integer type automatic 64 bit

I am using

for unsigned integer in my code. It has an advantage, if my computer support 64 bit,
will be
unsigned long long int
, otherwise, it is
unsigned int
. My code can run on both 32 bit and 64 bit platform.

Is there a similar signed integer? If the platform only support 32 bit, it is
, if the platform support 64 bit, it is
long long int

I can achieve this by setting a flag:

#ifdef USE_INT64
#define MY_INT int
#define MY_INT long long int

Then use
. However I need to define
and include this header for all my code. If there is something similar to
, it will be great.

Answer Source

If you want to tie your types directly to address width, then the types you are looking for are called uintptr_t and intptr_t. They are inherited from C standard library through <cstdint>.

Type size_t is not guaranteed to have the same size as pointer (it is smaller in general case), meaning that it can be used as a reference point for that purpose on your platform only.

Of course, the real question here is whether there's a good underlying reason to tie your integer type to address width. If not, a better idea would be to define your types directly through uintNN_t/intNN_t typedefs and forget about size_t and such.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download