Hey. Quick question. I'm wondering if i should use this:
for(unsignedchar i = 0; static_cast<int>(i) < 10; i++){
Or simply use this below (which is most effective?) I tried printing out the sizeof of the variable with both char and int, and i noticed the char is 3 bytes smaller, but i've never ever seen anyone use my method above. I do know it probably doesn't make much of a difference at all, but i am a perfectionist. I like my code to be as clean as possible, and take up as little memory as possible, even if i gotta write some extra code in order to do it. Which is best for the purpose? Thanks... And sorry if my english is bad.
the static_cast<int> will make your application take longer since there is an extra operation done every iteration.
If you are concerned about 1 byte versus 4 bytes you can do this: for(unsignedchar i = 0; i < 10; i++) {
However, when you declare something in that fashion, i will go out of scope once the loop is complete. Those extra bytes only exist for a moment and so I don't know if I'd consider it to really contribute to the amount of memory used by the application.
Also, I've heard that some processors will operate on an 8 bit (1 byte) number at the same speed as a 32 bit number (4 bytes). Therefore there is no speed increase here.
Oh okay.. Since static_cast converts the char on the number entered into the integer number entered, i thought i needed that in the loop. Well thanks!
Yeah i know it probably doesn't make much difference in memory.. For some reason that i don't know, i just want to make my codes the best possible, noticable difference or not.