View Single Post
  #8 (permalink)  
Old 09-04-2007, 08:27 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
I personally still don't get of why do you ask of "When to use one variables over another?" Because I'd say that their functionalities are different. Well, there are some of them those are similar, though, they have different sizes. So, it's either on how do you want to use the variable for, OR if you need more size for whatever information to be stored in a variable
In Java, yes, that is guaranteed. A quick comparison of what is comparable on my machine (format: Java = C/C++) -
boolean = _Bool/bool
byte = char
char = wchar_t
short = short int
int = long int (note that on my machine, a C/C++ int = a C/C++ long int, but a C/C++ int = a C/C++ short int on some other machines)
long = long long (C99 only, not valid with C++98)

As Bench wrote, C++ only guarantees that a char is at least 1 byte in size and each integral data type is at least the same size. The same is true for the real/floating-point types. Honestly, a char in C++ could be the same size as a long int, meaning that your long int would only be able to hold -128 to 127 (signed) or 0 to 255 (unsigned). Also, a char is often an int with a restricted range. As such, int is often preferred because using char causes conversion to take place at runtime, which can affect performance somewhat, depending on the efficiency of the instructions used for the conversion and the machine's abilities itself.

Quote:
Originally Posted by TeraTask View Post
I'm afraid I take a far more simplistic approach. In whatever language I'm working with, I go to the documentation and print out the minimum and maximum values. Then, when choosing a type, I choose the type that uses the smallest number of bytes and covers the entire range of possible values.
Yeah, that's nice to do, but in some languages, that can cause performance issues, as I said previously. The only reason they exist is for convenience IMHO. That way, a programmer doesn't need to "reinvent the wheel" by doing range checking herself/himself. In some languages however, no performance issues are noticed. For example, one can calculate 65536^256 in Python rather quickly to EXACT PRECISION. Often a program in certain languages such as Common Lisp, Perl and Python are amazingly fast compared to the equivalent program in the simpler, more machine-based C or C++ program. Of course, they were designed to be that way.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (09-04-2007)