Jump to content
  • Advertisement
Sign in to follow this  
Coz

When should I use data types with a known bit size?

This topic is 4861 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'm trying to figure out if it would be a good idea to use the data types from stdint.h like uint32_t instead of int or uint8_t instead of char.. I know its a good idea for data to be transfered over a network(since the two computers doing the transfer could have different sizes of data types) and that it's also a good idea when using bitwise operators. But would it be a good idea for portable code? for example, to port from a 32-bit machine to a 64-bit machine? Which other cases would using these data types makes sense? I guess it would be stupid to use them in every place you can XD

Share this post


Link to post
Share on other sites
Advertisement
In general, you should just use int. For 32-bit machines, it has ideal performance. As for 64bit machines, apparently int is still 32-bit there, and you'd have to use long long to get 64-bit ints.

The only place you'd need to use shorts is over massive arrays.

As for network traffic, I'd again say to use ints. That is, though, only if you're not using xml.

Share this post


Link to post
Share on other sites
For portability, you should probably use the 'at least' sizes (int_leastN_t with N={8,16,32,64}), because you only need to ensure that the types will hold at least the range that you want. Also, the int_leastN_t types are required, while the intN_t types are optional and may not be present. You could also use int_fastN_t because it is also required, and it lets you conserve memory (int_leastN_t) or runtime (int_fastN_t).

Share this post


Link to post
Share on other sites
Unfortunately, there is no easy rule. I generally use int unless the variable needs to be a specific size, and in that case I use the intN_t types. I never use char (except for text), short, or long because the use of those means that it needs to be a specific size.

Share this post


Link to post
Share on other sites
Quote:
Original post by Inmate2993
In general, you should just use int. For 32-bit machines, it has ideal performance. As for 64bit machines, apparently int is still 32-bit there, and you'd have to use long long to get 64-bit ints.


Nope. It's true that it's whatever is native on the machine with a minimum size of 16 bits.

The standard says that it should be whatever native size gives good performance, so it could be 32, 64 or even 128-bit. For all you know, it might be implemented as a 48-bit or some other weird number on an obscure piece of hardware.

Microsoft chose to use 32-bit ints on their 64-bit compiler, but that is a choice they made, not a requirement.

The long long type is C99, but isn't in C++. Same for the types in stdint.h (which are really cool). It has been proposed as <cstdint> for the C++ standard, but compiler support right now isn't there.

Boost has an implemention, though, but again, it isn't standard in C++ yet.

frob.

Share this post


Link to post
Share on other sites
Quote:
Original post by Coz
But would it be a good idea for portable code? for example, to port from a 32-bit machine to a 64-bit machine?

Which other cases would using these data types makes sense?


If you store binary data in a file you will want to use data types of a specific size to write code to read/write that file. (The other part of the story would be endianess.)

Whenever you do not need to have a specific size of the data type, use the native size of the system, like int. Or use the types that Extrarius suggested. In those cases you will only want the data type to behave the same on all systems.

If you are using data types to have a value wrap-around, i.e. a positive integer is incremented and becomes negative, you will also want to have that data type being a fixed size. Otherwise it will behave differently on some systems.

Also stuff like fast inverse of a floating point number
(found an article you can read here)
requires to have a data type of a specific size.

Another case where data types of a known size are used is direct hardware access, like the disk or network controller. Also the page tables your operating system manages have a fixed 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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!