Fixed sized datatypes.

J

Justin Barrett

Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?

Thanks in advance,
JB
 
V

Victor Bazarov

Justin said:
Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?

'unsigned short' is guaranteed to have _at_least_ 16 bits. If you need
_exactly_ 16 bits, you are out of luck. There are platforms out there
that simply don't have any types smaller than 32 bits. Your code could
never be ported there. If you don't care about that, you can settle on
making it "portable" to only those platforms that do have _a_ type which
is exactly 16 bits. Once you decide that that's what you want, you simply
create a header with conditionally compiled sections each of which defines
its own "justin_16bit_signed" type:

#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
....

Victor
 
D

David White

Justin Barrett said:
Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?

You can create a platform-specific header file containing typedefs for
fixed-sized types, e.g.,
typedef short Int16;
typedef unsigned short Uint16;

Use only the Int16 or Uint16 types in your code where you need 16-bit types.
When you move to a different platform, alter the header file as necessary
and re-compile.

DW
 
P

Pete Becker

Victor said:
#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
...

This should be conditionalized on the compiler, not the OS.

#ifdef _MSC_VER
....
 
E

E. Robert Tisdale

David said:
You can create a platform-specific header file
containing typedefs for fixed-sized types, e.g.,
typedef short Int16;
typedef unsigned short Uint16;

Use only the Int16 or Uint16 types in your code where you need 16-bit types.
When you move to a different platform,
alter the header file as necessary and re-compile.

This is very *bad* advice.
Use the [C99] standard header file

#include <stdint.h>

instead.
Then your code will port to every platform
that supports a C99 compiler
and integers of the sizes that you need.
 
V

Victor Bazarov

Pete Becker said:
This should be conditionalized on the compiler, not the OS.

#ifdef _MSC_VER
...

I do not agree, but I am not going to argue. It's up to the
programmer what to conditionalize on. For all we know it could
be conditionalized on some custom macro defined by the programmer
in the project settings.

V
 
D

David White

E. Robert Tisdale said:
This is very *bad* advice.

Why is it "*bad*" advice? It would work perfectly well. I agree that if
there's support for fixed-sized types provided by the language standard,
then that would be preferable, but your program isn't going to suddenly
crash or disappear in a puff of smoke if you follow my advice.
Use the [C99] standard header file

#include <stdint.h>

instead.
Then your code will port to every platform
that supports a C99 compiler
and integers of the sizes that you need.

Is this a C or C++ newsgroup? I can find no mention of stdint.h in the ISO
14882 C++ standard.

DW
 
J

Jack Klein

Hi I'm currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it's pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?

Thanks in advance,
JB

As at least one other poster suggested, do a Google search for the C
standard <stdint.h> header. Now others have pointed out that this was
added in the 1999 update to the C language standard, and is not
actually part of the C++ standard at all, and they are correct but
somewhat short-sighted.

But without a doubt it will be added to the next major update to the
C++ standard, probably as both <stdint.h> and <cstdint.h>. And right
now, quite a few compilers supply this header already, and it works
when they compile C++ programs as well as when compiling C programs.

Even if you can't find one already put together for your
implementation, you should either be able to write one from scratch or
find one similar enough that you can modify it.

Assuming that your C++ implementation provides 2's complement integer
types with widths of exactly 8, 16, and 32 bits, and I'll bet it does,
you can write your own <stdint.h> header that will be 100% C++
standard conforming except for the 64 bit integer types.

It's not that hard at all. I've done it for several C and C++
compilers.
 
V

Victor Bazarov

Jack Klein said:
[...]
But without a doubt it will be added to the next major update to the
C++ standard, probably as both <stdint.h> and <cstdint.h>. [...]

Probably as both <stdint.h> and <cstdint> (no .h)

V
 
P

Pete Becker

Victor said:
I do not agree, but I am not going to argue. It's up to the
programmer what to conditionalize on. For all we know it could
be conditionalized on some custom macro defined by the programmer
in the project settings.

The point still remains, however: the size of an integral type is
determined by the compiler, so whatever name is used for a magic macro,
ultimately the choice has to be based on the compiler that's being used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top