Advertisement

CHAR or DWORD?

Started by May 06, 2000 06:33 AM
13 comments, last by TUna 24 years, 7 months ago
Hi In most programming projects I uses the smallest variable size possible to conserve ram, so if a value will only ever be between 0 and 10 I always use a char, now I''m wondering if this is not such a good idea with game programming? Will the code execute faster if I use all DWORDS??? Thanks
I''m not sure about this one, but ising all DWORDs will use up more memory, thus *probably* slowing it down.

DWORD = 32 bits
char (BTYE) = 8 bits

Just incase you were wondering, following shows how much data each variable type uses.

bool = 1 bit
char = 8 bits
int = 16 bits
float = 32 bits
double = 32 bits (I think)
long = 52 bits (I think)

I always use ints at first, and then optimise it at a later date (i.e. when your only getting 3 fps ).

MENTAL
Advertisement
actually thats not true:

char = 8 bits
short int = 16 bits
long int = 32 bits
by default, ints on 32bit processors are 32 bits, so
int = 32 bits

just becuase a DWORD is larger than a char does not mean it is slower, it may be faster to acces becuase it is better aligned in the memory, for example, if you have this:
struct Test
{
char a;
}

you might think that the size of this is only 1 byte, but in reality, by default, MSVC will pad this struct up to 4 bytes becuase this will increase speed.

Im not an expert, and im not totally sure of the sizes of floats and doubles, but i am very sure of what i have stated.
I''ll back most of Mr. Anon up, on 32-bit processors (Pentium, PII, PIII, some others) the processor handles 32-bit size data better. On the Athlon it''s 64-bit, so that handles 64-bit data better. On the other hand, the sizes could be something to consider if the target system has only 8mb RAM - but thats not really very realistic nowadays, which is partly why some programmers have moved away from fixed number types.
It is also true that int''s default to 32-bit in 32-bit enviroments.
hence:
int = 32-bits
long int = 32-bits
short int = 16-bits
float = 32-bits
long double = 80-bits
char = 8-bits
bool = 8-bits
even though the boolean value only needs one bit, it cannot be addressed.

If I''m wrong, I''m sure someone will correct me.

-Mezz
That list seems to be correct, but isn''t a long double 96 bits? And how many bits does an "ordinary" double have? 64?

/. Muzzafarath
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
a long double is 96.

sizeof(long double) == 12
12 * 8 (bits in a byte) == 96

and a DWORD would be equivalent to an int on a 32 bit system, and a char gets upgraded to an int in pure C (not sure if C++ does), so really, they are basically the same
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Advertisement
i''m not quite sure why i''m doing this, but a double is 64 bits and a long double is 80.. this whole topic is moot though - many unix systems implement char as a 16 bit data type, BOOL under windows is 16 or 32 bits whereas the regular bool is still 8, etc.
chars don''t get "upgraded to pure int," i''m not quite sure where you got that from, but its incorrect.. anyway, thats my two cents.


--
Float like a butterfly, bite like a crocodile.

--Float like a butterfly, bite like a crocodile.
why the hell is 8 bits used for a boolean when you only need 1? there is some optimization document out there that says that the PIII and II can handle 1 bit operations with ease.
Memory alignment!

actually, i thought that MSVC used 32 bits for a bool. sure, its a huge waste, but the memory is aligned, and a lot faster to work with in the end.

and, long doubles are 80 bits, not 96. Though, most x86 compilers dont use them anyhow (MSVC just maps them down to 64)

===============================================
"Tell brave deeds of war."
Then they recounted tales, -- "There were stern stands And bitter runs for glory."

Ah, I think there were braver deeds.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
BOOL is mapped to 32 bits in the Win32 headers, but the bool C++ keyword is genrally 8 bits.

Just remember that if you use a DWORD when a CHAR will do, you''re using 4 times the amount of memory, so are 4 times more likely to cause a cache miss.

This topic is closed to new replies.

Advertisement