Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hello

Started by cyber857 Sep 21, 2005 at 2:53 PM 2 replies 750+ views
Original Post
cyber857
cyber857
I going to be useing this is reguards to a game but it is a general question i have about a TYPE in C++ lets say im storing an INT for some stat of a charicture in game i.e. strength so i have int str; now in reguards to memory usage if str is always between 0 and 255, which for my project it would be cant i assign it to TYPE char? i.e.... unsigned char str; Does this work? (someone told me i have to have unsigned in an email, (as i undetand it to make all the values + other wise half would be negative.) can i then still deal with the varible str as i would a int? do i have to type cast when doing math with it? i.e. dmg = (int)str + dex - enemy_def * acc; ummt that might not be the way to type set C++ but as i said im still learning it and im sure you have some way to do it. my understanding is that INT is 4-times as big as CHAR so if i want to save info to a memory card that has a fixed amout, say on some sort of game consol its best to do it this way. Also i wouldn't mind learning the best way to streamline things even if you have to make a bit unglyer code. comments are greatly welcome. btw if i overloaded my orginal question was can i just define a varible str as a unsigned char and hold a value in it of 0-255 and type set it when i need to do math (do i need to type set it?). thanks, Tom.
Agony
Agony
char can indeed be treated as a number type. In fact, it is a number type. Various utility functions might be designed to act as though it is a character, but as far as the compiler is concerned, it is a number type, and thus math operators and such will work as expected.

Given that, though, I would suggest that you use int anyway. True, a char is (typically) four times smaller, but in many stages of execution (such as when the number is being operated on by the processor), it is going to be the same size as an int anyway. The only thing you might be saving is a few bytes in RAM or on the hard drive (if you save it to a file). And until you get pretty large programs, a few bytes is going to be majorly insignificant.

Stick with good style and quality of code before ever worrying about performance. As you become better and more experienced at programming, you might begin to develop a good intuition for when you can worry about performance early on, but as you're learning, it is a very wise thing to worry far far more about good style/readability than it is about performance. And in the later stages of learning, when you do begin to worry more about performance, it will mostly focus around general algorithmic issues, rather than minor things such as "Should I use chars instead of ints?" or "Should I use bit-shifts instead of multiplies?" Only when you really really know what you're doing should you worry much about "micro-optimizations" such as this, because at that point, you'll know better whether it is indeed worth it or not.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
dmatter
dmatter
you can use an unsigned char to store values from 0-255. But be aware that if you write that varialbe to a file it would (likely) be written as a character symbol, this is only a problem if the file is supposed to be human-readable (a person would see a symbol rather than a number) - ofcourse when reading the file again later the symbol would be read back into the char and used a number again.

In C++ you would not need to type-cast a char to an int in order to perform operations on it.

I think that in this case case, the decision to use a char rather than an int for the sole purpose of saving memory is barely worth worrying about - You would (probably) save 3-bytes of memory which is practicaly nothing on modern computers and consoles.

NOTE: Please do-not assume that a char is a signed char, it might well be an unsigned char! The decision is completely up to the compiler not the C++ standard!
ZQJ
ZQJ
As far as I remember (from somewhere deep in the back of my mind) chars and unsigned chars are converted to ints (and presumably unsigned ints) when doing arithmetic so you don't have to type cast them to ints to avoid overflows. I might be wrong so I recommend experimenting. Otherwise, what Agony said. You shouldn't worry about it unless you have a space problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.