Sign in to follow this  
GraySnakeGenocide

Are these variable types used alot in game programming?

Recommended Posts

The variables in question being:

short
ushort
long
sbyte
byte
ulong
uint

byte is the only one i've used from this list, doing something with the color palette.

But these all look like overkill, instead of just using the floats/decimals/ints/doubles of the programming world. Is there any specific reason as to why I should learn these, or should I not worry about them for a while?

Share this post


Link to post
Share on other sites
Telastyn    3777
[quote name='GraySnakeGenocide' timestamp='1302351657' post='4796304']
The variables in question being:

short
ushort
long
sbyte
byte
ulong
uint

byte is the only one i've used from this list, doing something with the color palette.

But these all look like overkill, instead of just using the floats/decimals/ints/doubles of the programming world. Is there any specific reason as to why I should learn these, or should I not worry about them for a while?
[/quote]

I've seen longs and ulongs used before, but let's be honest here: primitive data types aren't exactly the most complex thing in the world. Know that they exist, look them up when you've got a compelling need (like interop) to use something other than char/int/double.

Share this post


Link to post
Share on other sites
rip-off    10979
For binary serialisation, you will use them. Elsewhere, it won't really matter. In some cases using a smaller type can bring large memory bandwidth gains, but it is critical not to prematurely optimise these things. Overflow is a much more subtle problem compared with a noticeable slowdown.

Share this post


Link to post
Share on other sites
Ezbez    1164
They are used in embedded programming and in highly bandwidth-limited applications, like some networking code. For most PC applications, no you don't need them.

Share this post


Link to post
Share on other sites
PropheticEdge    150
Yeah, you'll run into them, but it's rarely an issue. All those data types are quite close, and differ mostly in the number of bits they hold. You should be able to find reference for the compiler you're using as to how it interprets the various data types.

Share this post


Link to post
Share on other sites
TheTroll    883
[quote name='Hodgman' timestamp='1302359005' post='4796346']
Low-level systems stuff, and when dealing with file-formats.

For day-to-day stuff you've got int, float etc ;)
[/quote]

This is one of the reasons why we have to have so much memory now days. Use the smallest data type that will accomplish the job. Anything else is just a waste of memory.

Share this post


Link to post
Share on other sites
Hodgman    51344
[quote name='TheTroll' timestamp='1302367636' post='4796399']
This is one of the reasons why we have to have so much memory now days. Use the smallest data type that will accomplish the job. Anything else is just a waste of memory.[/quote]If I've got a variable that has a range of 0-9001, I'd likely still put it in and [font="'Lucida Console"]int[/font] rather than a [font="'Lucida Console"]short[/font], unless there's a good reason to reduce the memory usage (e.g. serialisation across network/disk/system-bus). Normally you just want to stick with the CPU word size to avoid packing overheads.

Share this post


Link to post
Share on other sites
ravengangrel    406
[quote name='TheTroll' timestamp='1302367636' post='4796399']
This is one of the reasons why we have to have so much memory now days.
[/quote]
You almost make it sound as if it was a bad thing!

[quote] If I've got a variable that has a range of 0-9001, I'd likely still put it in and [font="Lucida Console"]int[/font] rather than a [font="Lucida Console"]short[/font], unless there's a good reason to reduce the memory usage (e.g. serialisation across network/disk/system-bus). Normally you just want to stick with the CPU word size to avoid packing overheads.
[/quote]
Indeed, there can be some reasons to use larger variables than needed (alhough reducing memory usage is not the only reason to avoid them; think of a video player, even a 1920p video frame isn't big for today standards. Would you dare to work with only ints to, say, adjust contrast and brightness at a low level?)

Ignoring the fact that you are oversimplifying, this is a discussion in a beginner's thread where some dude is asking whether he must learn the different basic types of his choice.
I find shocking me that the message most of you are transmitting is: "D'oh, who cares".
Yeah, perhaps most of the time you can just throw an int. Perhaps most of the time it's even better. But you do it for a reason, not because you don't have another choice. In the same way that TheTroll chooses to use the smallest possible data type. You can argue against the wisdom on that, nowadays that we count RAM in Gb and hard discs in TB, but the fact is that both of you know what you need, balance the advantages and the disadvantages, and make your choice, be it better or worse.

GraySnakeGenocide, you can probably do almost the same things without knowing those data types (not everything, at least not in a overly complex way). However those types are there for a reason. I personally don't find very compelling to work with pro's who can't say the max value of a 32 bit integer (I don't need the exact value, but should know it's around 2000 millions if it's signed).
Probably knowing the exact values is overkill, but you should get a grasp of them, and know the reason (signed: -2^(n-1) to (2^(n-1) - 1); unsigned: 0 to ((2^n) - 1).
You should know that 32767 + 1 = -32768 when you are using ushorts, and that 65535 becomes -1 when converting a short to an ushort. And, most important, you should know WHY.

The time you spend learning this will be saved in frustrating clueless debugging sessions

Dude, I feel so 90-ish now. I'll better take a shower.

Share this post


Link to post
Share on other sites
[quote name='GraySnakeGenocide' timestamp='1302358546' post='4796340']
So the verdict is rarely?
[/quote]
Rarely, but I use my socket wrenches rarely too. I may use my hammers and my ints frequently, but when I encounter a certain problem or a certain need, I really want my socket wrenches in a place I can find them.

Example: long

long long = a 64 bit integer.
unsigned long long = an unsigned 64 bit integer.

If you are dealing with something were regular 32 bits are not enough, it would be horrible to have to make your own socket wrenches from duck tape and leftover banner peels that's incompatible with the rest of the tools because the language doesn't have it supported.

Some of the variables you mentioned are probably just typedefs to make things easier to read and write.
[color="#1C2837"][size="2"]short
ushort = unsigned short[/size][/color]
[color="#1C2837"][size="2"]byte
sbyte = signed byte[/size][/color]
[color="#1C2837"][size="2"]long
ulong = unsigned long[/size][/color]
[color="#1C2837"][size="2"]uint = unsigned int[/size][/color]

[size="2"][color="#1C2837"]Alot of 3rd party libraries typedef their own variables to serve this same purpose. I see alot of _int64, _uint64, t_int64, t_uint64, Sint64, Uint64, uint64 sint64, UINT64, SINT64, ns::Int64, ns::Uint64, from multiple different libraries.[/color][/size]
[size="2"][color="#1C2837"]Sooner or later, you have to ask yourself, wouldn't it be alot better if the standard library added these variables in a simple typedef, so every 3rd party library in existence doesn't have to define its own, and so we can get a consistent naming scheme?[/color][/size]

Share this post


Link to post
Share on other sites
MeshGearFox    158
You're probably okay with just ints when developing. Increase or decrease the amount of space you need to. Technically using an int when you could use a short is wasteful but the amount of memory you'd save is sort of trivial. I'm thinking unnecessary copying of large containers and objects is going to be a much bigger issue.

Although, I think I've used shorts for small numbers before as a sort of indicator to myself that they were only supposed to hold small values.

Share this post


Link to post
Share on other sites
Daaark    3553
[quote name='Hodgman' timestamp='1302369664' post='4796411']
Normally you just want to stick with the CPU word size to avoid packing overheads.
[/quote]WINNER. [/thread]

Share this post


Link to post
Share on other sites
ravengangrel    406
[quote name='Daaark' timestamp='1302983481' post='4799233']
[quote name='Hodgman' timestamp='1302369664' post='4796411']
Normally you just want to stick with the CPU word size to avoid packing overheads.
[/quote]WINNER. [/thread]
[/quote]

1) The thread was about [u][b]LEARNING[/b][/u] basic variable types, not about using them.

2) This is not /b/.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this