I know that type "int" is not guaranteed to be a specific size, as well as all of the other primitives, but is there a quick and easy way to dynamically create custom data types that isn't going to break the program when switching to a non-standard platform?
For example, I like to define char, short, int, long int as int8, int16, int32, int64, respectively, and then only use the minimum type required for all possible values held in the variable (if I know the domain prior to defining it) to minimize wasted space. The problem lies in the fact that I may need a variable to hold 64 bits, and define it as int64, but this will be erroneous on a system that defines "long int" as only 32 bits.
I would create typedefs for the different types you want to use. Then, when you change platforms, you only have to update the typedefs to use the proper built-in type for that platform.
Since you can't force a specific size in a platform independent way, that will be the best option I can think of.
However, you should ask why you think you need to do this in the first place. Are you sure you would be wasting space? Are you sure that there won't be other performance penalties for not using the default sizes for the particular platform?
May-be you can use the <cstdint> (or <stdint.h>) headers.
I believe there are some typedefs stored somewhere in the Windows.h mess. So on Windows you wouldn't have to worry about it.
All code I've seen that uses this will have something like:
Obviously there may be more variants of processors, either combined into a single #if, or using a #if/#elsif/#else branch-tree.
typedef long int int64;
typedef long long int int64;
You can of course also have different include files for different architectures, so you have a "i386/types.h", "x86_64/types.h", "ppc32/types.h", "ppc64/types.h", "arm/types.h", etc, etc.
If you are just starting on a project, you may want to just define the types once, and then let it be a future problem to solve the actual "how to we make this work for multiple architectures".
But as stated above, there is no "one solution works for everything" - since different processor architectures and compilers have different "ideas" about what is what size.
The Boost library has <boost/cstdint.hpp>, which contains a lot of typedefs.
That's kind of what I was looking for, I guess. I was really hoping there was something like #ifdef ___64BIT___ that would work for all 64-bit processors, and one for 32-bit, etc.
Originally Posted by matsp
Well, one of the difficulties here is that not all 64-bit architectures/compilers have the same "rules" - for example MS VC++ for x86_64 has "long" as a 32-bit integer, whilst gcc for x86_64 (at least on Linux) has "long" as 64-bit. So just simply saying "the processor is 64-bit" doesn't answer all the questions, if you see what I mean.
Originally Posted by drrcknlsn
Further, gcc supports "long long", whilst MS VC++ uses "_int64" to indicate a 64-bit integer.
So it's not just the processor, it's which compiler you are using as well.
There are probably other compilers with further different standards.