Archived

This topic is now archived and is closed to further replies.

64 bits and the C++ standard

This topic is 5376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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_BIT

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites