Jump to content
  • Advertisement
Sign in to follow this  
Valor Knight

int or short/long?

This topic is 4486 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

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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.
typedef unsigned char  UINT8
typedef unsigned short UINT16
typedef unsigned int UINT32
typedef signed char SINT8
typedef signed short SINT16
typedef 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]

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 [ code ] [ /code] tags if you need to escape something.

[Edited by - Shannon Barber on April 5, 2006 11:35:21 PM]

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!