Archived

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

Which data type to choose...

This topic is 5112 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

In C++ in a 32 bit environment... If I know something will need less than 32 bits, should I not use an int? In other words, is it generally a good idea to use ints as much as possible (unless you only need a bool, of course), or if you should use the smallest thing possible? I am working with the DirectInput key enumerations, which don''t even reach 256. Should I use an int to handle them in my DirectInput wrapper, or an unsigned char? I would want to go with int, because I beleive its easier for the CPU to work with, but what do you think? Oh yeah, I am not doing this for added speed (the difference is probably tiny), I am just wondering which is generally the better idea.

Share this post


Link to post
Share on other sites
I generally make it a char if it is some key value, or an int if it represents a number. I never put numbers in chars except for rgb values or when using an int takes up too much space.

--------
"Hey, what about those baggy pants you kids are wearin'' these days? Aren''t they hip and cool and poppin'' fresh?"-Peter Griffin
"Everytime I see an M followed by a dollar sign I just say "oh god" and go to the next post."-Neurokaotix
the D programming language|google|msdn|XLib reference manual

Share this post


Link to post
Share on other sites
bools are simply int''s in most C++ implementations, so even using bools instead of ints is useless in most cases .

Also, if you do this in msvc:

struct SomeKey
{
char Test;
};

And then check the sizeof(SomeKey), it will return 4 due to 4-byte alignment, so using a single char would be useless, since it wastes 4-bytes anyways (unless you use the #pragma pack() preprocessor of course).

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
bools are simply int''s in most C++ implementations, so even using bools instead of ints is useless in most cases .

Also, if you do this in msvc:

struct SomeKey
{
char Test;
};

And then check the sizeof(SomeKey), it will return 4 due to 4-byte alignment, so using a single char would be useless, since it wastes 4-bytes anyways (unless you use the #pragma pack() preprocessor of course).


I always thought that a bool was an unsigned char... and it says in the VC++.NET help file that it is 1 byte. And about the padding to make it 4 bytes, you may very welll be right, I know that the compiler does this to classes to make them match up with 8 bytes. Either way, I will probably be using pure ints. Thanks for the advice!

Share this post


Link to post
Share on other sites
Sorry, you are correct..

bool == char (1 byte), I was thinking BOOL .

printf("%d %d",sizeof(bool),sizeof(BOOL));

bool = 1, BOOL = 4, sorry about the confusion. I never use bool or BOOL, I always just use a char, that way if I ever modify my program to work with anything besides bools, i don''t have to change data types.

For example: You write a GUI, and store a "Mouse down" as a bool... later you decide you want to store WHICH mouse button is down, now you have to go back and change the bool to a char, when if you used a char to begin with, no changes are required, and your old code works fine (if (button==true) and if (button) both are true whether button is 1,2,3,4,5, etc, so storing which button will not break existing code!).

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
Sorry, you are correct..

bool == char (1 byte), I was thinking BOOL .

printf("%d %d",sizeof(bool),sizeof(BOOL));

bool = 1, BOOL = 4, sorry about the confusion. I never use bool or BOOL, I always just use a char, that way if I ever modify my program to work with anything besides bools, i don''t have to change data types.

For example: You write a GUI, and store a "Mouse down" as a bool... later you decide you want to store WHICH mouse button is down, now you have to go back and change the bool to a char, when if you used a char to begin with, no changes are required, and your old code works fine (if (button==true) and if (button) both are true whether button is 1,2,3,4,5, etc, so storing which button will not break existing code!).


Someone said in another thread I found while searching for bools that the compiler can perform optimizations if it knows that a variable is a bool. Do you know anything about this? If it doesn''t, then it would just be wasting 3 bytes because of padding. Either way, the speed difference would be minimal. And are you sure about the padding thing? It would make sense, considering the compiler pads classes, too.

Share this post


Link to post
Share on other sites
Hmmm.... Don''t most compilers these days default to long for ints these days anywayz? A quick check with gcc does, and I''m pretty sure that VC++/NET does also. If you want the smaller representation, specify short. Also, a single char may be padded, but I would think that an array of chars will be packed in together (with maybe some padding at the end) which is how I usually see this done.

unsigned char keys[256;

peace and (trance) out

Mage

Share this post


Link to post
Share on other sites
From my experience in assembler (which is relatively short in reality), the opcodes are generally alot smaller for bytes and words (16 bit word size that is) than it is for dwords (32 bits).

In reality however, as long as it isn''t chewing up too much memory (either in the code stream or as data storage), it really doesn''t matter. Like they say ''Premature optimization is the key to all evil'' (although sometimes optimization isn''t really premature, just early. Like for graphics intensive code, for example).

< krysole || krysollix >
sleep, caffeine for the weak minded

Share this post


Link to post
Share on other sites
quote:
Original post by Mage2k
Hmmm.... Don''t most compilers these days default to long for ints these days anywayz? A quick check with gcc does, and I''m pretty sure that VC++/NET does also. If you want the smaller representation, specify short. Also, a single char may be padded, but I would think that an array of chars will be packed in together (with maybe some padding at the end) which is how I usually see this done.

unsigned char keys[256;

peace and (trance) out

Mage


Yes, ints are 32-bit. And I did a quick google search and found that almost all compilers do pad. If it is an array, they wouldn''t, because of pointer math (address[offset]), but they would probably pad at the end.

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
Sorry, you are correct..

bool == char (1 byte), I was thinking BOOL .


Since the OP did not specify what particular environment we are talking about, we can only assume what the standard guarantees: A bool is at least as large as a char (which is at least 8 bits) and at most as large as a long (which it at least 32 bits). Beyond this we have no guarantees.

With regards to speed, note that an int is typically the same size as a memory word (32 bits on a 32-bit nmachine). This, AFAIK, means that computation with ints is typically always as fast as integer arithmetic will get on your machine (though on the other hand, if your chars are 8 bits and your ints are 32 bits, moving ints around involves four times as much memory to copy). (When I say typically I mean to say that, AFAIK, this can generally be assumed the case, but even this is not guaranteed.)

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
quote:
Original post by Ready4Dis
Sorry, you are correct..

bool == char (1 byte), I was thinking BOOL .


Since the OP did not specify what particular environment we are talking about, we can only assume what the standard guarantees: A bool is at least as large as a char (which is at least 8 bits) and at most as large as a long (which it at least 32 bits). Beyond this we have no guarantees.

With regards to speed, note that an int is typically the same size as a memory word (32 bits on a 32-bit nmachine). This, AFAIK, means that computation with ints is typically always as fast as integer arithmetic will get on your machine (though on the other hand, if your chars are 8 bits and your ints are 32 bits, moving ints around involves four times as much memory to copy). (When I say typically I mean to say that, AFAIK, this can generally be assumed the case, but even this is not guaranteed .)


Sorry! I didn't say it in the original post, but I said I used VC++.NET in a later reply. And, as I said, a bool is 1 byte according to its help file. But, because of padding, I will just use ints. Thanks a lot for all of your (quick) input!


[edited by - PlayGGY on December 13, 2003 12:20:53 AM]

[edited by - PlayGGY on December 13, 2003 12:23:26 AM]

Share this post


Link to post
Share on other sites
This is premature optimization. Use whatever makes sense. If you are reading/writing binary data (from a file or socket say) then you don't want to use just ints. But if you're dealing with numbers then using ints universally isn't really a big deal I think.

Don't try to replace bools with ints though. Some people in the D language do that (but use the bit datatype), its rather asinine.

[edited by - antareus on December 14, 2003 1:03:25 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by antareus
This is premature optimization. Use whatever makes sense. If you are reading/writing binary data (from a file or socket say) then you don''t want to use just ints. But if you''re dealing with numbers then using ints universally isn''t really a big deal I think.

Don''t try to replace bools with ints though. Some people in the D language do that (but use the bit datatype), its rather asinine.

[edited by - antareus on December 14, 2003 1:03:25 AM]


I am not doing for optimization, as I said, I am doing it to make my code more consistent. And the only thing wrong with replacing bools with ints that I see is now the variable can no longer only have 2 values.

Share this post


Link to post
Share on other sites