int or short/long?
Stupid question, I know but I remember reading that int is unreliable, is it better to use short or a long depending on expected size and ignore ints?most of the time I am using unsigned longs or shorts anyway, just would be good info to know. thanks.
Quote:Original post by Valor Knight
Stupid question, I know but I remember reading that int is unreliable, is it better to use short or a long depending on expected size and ignore ints?most of the time I am using unsigned longs or shorts anyway, just would be good info to know. thanks.
short [int], int, and long [int] all can have different sizes on different platforms and compilers.
For any compiler and platform you're likely to care about (starting with GameBoy GBA, and going all the way to Windows-64), short is 2 bytes, int is 4 bytes. On most platforms (including Windows-64), long is 4 bytes, except some LP-64 systems (like Linux on 64-bit CPUs) where long is 8 bytes. "long long" is almost always 8 bytes, where supported. size_t is always the same size as void * / char *, but may be 4 or 8 bytes.
Thus, for pretty well portable code, here's a cheat-sheet:
char = 1 byte
short = 2 bytes
int = 4 bytes
long long = 8 bytes
size_t = integer the size of a pointer
Thus, for pretty well portable code, here's a cheat-sheet:
char = 1 byte
short = 2 bytes
int = 4 bytes
long long = 8 bytes
size_t = integer the size of a pointer
The standard makes the following guarantees about the sizes of data types:
sizeof(char) = 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) [<= sizeof(long long)]
Signed and unsigned do not affect the size of a variable. (The long long type is standard in C99 and supported as a C++ non-standard extension in GCC 3.x, GCC 4.x, and VC8+.)
Note that VC and GCC disagree on the size of the long data type on 64 bit architectures.
sizeof(char) = 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) [<= sizeof(long long)]
Signed and unsigned do not affect the size of a variable. (The long long type is standard in C99 and supported as a C++ non-standard extension in GCC 3.x, GCC 4.x, and VC8+.)
Note that VC and GCC disagree on the size of the long data type on 64 bit architectures.
I'm sure ints were 2 bytes when I first started programming. 'integer' in VB is still 2 bytes nowdays.
The safest way is to use some typedef (or heaven forbid a #define). e.g.
I believe that some libraries out there already do exactly this.[cool]
The safest way is to use some typedef (or heaven forbid a #define). e.g.
typedef unsigned char UINT8typedef unsigned short UINT16typedef unsigned int UINT32typedef signed char SINT8typedef signed short SINT16typedef signed int SINT32
That way if you find yourself on a platform where the sizes are different, you only have to fix it in one place. You can even use C_ASSERT (compile time assert) to check that the sizes of them are what you expect.I believe that some libraries out there already do exactly this.[cool]
#include or depending on which one is applicable. Then wallow in the glory of uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t.... No need for ad hoc defines anymore!
The post above is supposed to say stdint.h, but this forum is not smart enough to automatically translate what's written into a form that can later be displayed.
Edit: The board supports HTML in post but I beliebe it is disabled for AP. You should be able to use [ source ] [/ source ] or
[Edited by - Shannon Barber on April 5, 2006 11:35:21 PM]
Edit: The board supports HTML in post but I beliebe it is disabled for AP. You should be able to use [ source ] [/ source ] or
tags if you need to escape something.[Edited by - Shannon Barber on April 5, 2006 11:35:21 PM]
int is not unreliable, "Plain ints have the natural size suggested by the architecture of the execution envirorment" (taken directly from the standard). You can't be promised how big it's, it might be 1 byte or 255, but on most current architectures it will be 4 bytes, and in some cases 8. Apart from assuming stuff you can use the Boost's library for integer types, or you can create a simple little utility class yourself using template meta-programming and std::numeric_limits<T> (in case we are talking C++ and not C, you haven't said that). Generally if you need a "fast", general-purpose integer type use int.
EDIT: Check this site for more info about using Boost to solve the problem. If you for some reason can't use Boost you will have to program something like that yourself.
EDIT: Check this site for more info about using Boost to solve the problem. If you for some reason can't use Boost you will have to program something like that yourself.
If you need an integer with a specific number of bits, don't use short, int, or long. Use types with the size in the name (as suggested above).
I never use short or long, because using them means that I need a specific size.
I never use short or long, because using them means that I need a specific size.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement