int, float, long, DWORD, all the same size?

Started by
21 comments, last by billybob 21 years, 4 months ago
i thought a long was a integer with a bigger capacity, float had more bytes, and DWORD was a big honkin variable. but they are all 4 bytes??? in otherwords, if you declare a variable as a long it has the same maximum value as a variable defined as int?
Advertisement
on 32-bit architectures, yes.
what? im curious now, please explain
A DWORD is simply an unsigned long, so it is the same size as a long.
''int'' is generally a type whose size equals the size of the word on a particular processor.

thus, on dos/win16 int is 2 bytes long, and on win32 it''s 4 bytes long.

for dos/win16 architectures, long used to refer to a doubleword, and was 4 bytes long. on win32, it''s still a referred to as a doubleword, following the convention that a word is two bytes long, even though technically a word on 32-bit processor is 4 bytes long. long kept its 4 byte length.

so, int and long on win32 have the same size.
if you really want an int to be 16-bit, do this:
short int a_variable;

this comes out to a 16-bit value and on both 16 and 32-bit compilers.

long int another_variable;

will be 32-bit on both 16 and 32-bit compilers. Try this program to see how many bytes your compiler gives each variable:

  #include <iostream>using namespace std;int main(void) {	cout << "The size of an int: " << sizeof(int) << "\n";	cout << "The size of a long int: " << sizeof(long) << "\n";	cout << "The size of a short int: " << sizeof(short) << "\n";	cout << "The size of a float: " << sizeof(float) << "\n";	cout << "The size of a double: " << sizeof(double) << "\n";	cout << "The size of a long double: " << sizeof(long double) << "\n";	cout << "The size of a char: " << sizeof(char) << "\n";	cout << "The size of a bool: " << sizeof(bool) << "\n";	return 0;}  

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Shouldn''t a BOOL variable be 1 bit (I don''t mean byte) long ?

Kamikaze
Kamikaze: Nope. Although a bool only stores true/false values, it uses a byte for storage. And a BOOL is only an MS typedef for unsigned char (or something similar, no reference near me now).

nobodynews: Testing the size of a char is superfluous, as sizeof returns the size of its argument in chars, i.e. sizeof(char) will always return 1 regardless of architecture.


The only things guaranteed about the sizes of built-in types are (from memory, don''t quote me on this):
A char is at least 1 byte, and is shorter than or equal to a short int is shorter than or equal to an int is shorter than or equal to a long int.


In the good old days whenever a type was required in a declaration the presence of an int was implicitly assumed whenever necessary, so most people didn''t write short int or long int, but merely short and long. This has been made a standard way of declaring the types short int and long int in both C and C++.


And for all those ugly all-uppercase types (BYTE, WORD, DWORD, BOOL, etc.), they are merely MS typedef for builtin types, with BOOL=unsigned char (possibly int actually, I don''t remember), WORD=unsigned short, DWORD=unsigned int on modern systems.


-Neophyte
quote:Original post by Kamikaze15
Shouldn''t a BOOL variable be 1 bit (I don''t mean byte) long ?

Kamikaze

Since a variable''s address is given in bytes, it''s impossible to address individual bits.
quote:Original post by Neophyte
Kamikaze: Nope. Although a bool only stores true/false values, it uses a byte for storage. And a BOOL is only an MS typedef for unsigned char (or something similar, no reference near me now).
Correct, a BOOL is a typedef, but a bool is a standard type. Its actual length, I think is implementation specific, and is only required to hold one of two possible values.

This topic is closed to new replies.

Advertisement