Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TUna

CHAR or DWORD?

This topic is 6918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!