64 bits and the C++ standard

Started by
5 comments, last by 255 21 years, 1 month ago
I want some of my variables to be compiled 64 bit on 64 bit machines and 32 bit on 32 bit machines. The Microsoft API documentation tells me to use Microsoft datatypes such as INT_PTR but I want to make portable code according to the standard. How is this done? What size will longs, ints, words, chars etc. be on 64 bit? Any other tips for portable code?
Advertisement
I don''t know what size the variables will be on 64-bit computers. Maybe int stays at 32-bit (although I remember old compilers having 16-bit ints) and long will be 64-bit. But anyway, all you need to do is use typedefs.

On 32-bit computer, you''d use
#ifndef MACHINE_32_BITtypedef char int8;typedef short int16;typedef int int32;//and so on#elseif//put typedefs for 64-bit machines here#endif 

Now just use int8, int32 etc. rigorously instead of char, int etc. and consistency will be guaranteed on all platforms.
quote:Original post by 255
The Microsoft API documentation tells me to use Microsoft datatypes such as INT_PTR but I want to make portable code according to the standard.

It''s not going to be very portable if the valid values vary from platform to platform!

You might want to look into the standard <limits> header, or the Boost Integer library.
quote:Original post by 255
I want some of my variables to be compiled 64 bit on 64 bit machines and 32 bit on 32 bit machines. The Microsoft API documentation tells me to use Microsoft datatypes such as INT_PTR but I want to make portable code according to the standard. How is this done? What size will longs, ints, words, chars etc. be on 64 bit? Any other tips for portable code?


According to the C and C++ standards, the sizes of the various datatypes are completely up to the compiler-writer (with some restrictions). There is no "standard" way to specify sizes.

The best way to do it is to create your own types according to your needs. Include definitions for each compiler/machine combination. For example, you want a type whose size is the word size:


  #if defined( _MSC_VER )    #if defined( _M_IA64 )        typedef __int64 Word;    #else        typedef __int32 Word;    #endif#elif defined( GCC )    ...#endif  



[edited by - Jambolo on March 24, 2003 1:13:15 AM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Thanks for the replies.
C99 already includes a stdint.h header. The C++ equivalent will likely be added. To have firm guarantees about the size of an integral type, you will have to use int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t. Sizes not available on a given platform will not be defined and thus yield a compile-time error.

Again, following C99 decisions, the ''possibly larger than a long'' type is most likely to get named long long.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
If you''re using an ancient compiler, or want to be sure of all this junk...

Do something like

#if (sizeof(int) == 4)
#define int32 int;
#elseif (sizeof(long) == 4)
#define int32 long;
#endif
...etc...

This topic is closed to new replies.

Advertisement