I would like to optimize Arduino RAM utilization.
I use 'Int' variables instead 'Byte' because it seems to me more practical to use in comparisons, conversions...
I would like to know if I'm improving something using 'uint8_t' instead 'Int'... because some variables that I'm using are in 0-255 range.
Please let me know
Thanks on advance
Best regards
Pedro Ferrer
I use 'Int' variables instead 'Byte' because it seems to me more practical to use in comparisons, conversions.
How? The type of a variable matters little when constructing an if statement, for instance. When converting from one type to another to another, the types matter, but converting from byte to float is exactly the same as converting from int to float.
This statement needs some additional explanation, I think.
The natural word size of the AVR's RAM datapaths is 8 bits, so a "byte" type (8 bits) will be fetched into a CPU register more quickly than an "int" type (16 bits), and an "int" will require more program memory to hold the extra instructions to fetch and manipulate it.
using ITOA... Integer to Ascii... is some of the reason that's why I'm using 'Int' instead 'Byte'
You may need to learn about casting, then. Typically, a value will be implicitly cast to a larger type, but must be explicitly cast to a smaller type.
So, a byte (or uint8_t) will be implicitly cast to an int for itoa() to use, but you might need to explicitly cast the int returned by atoi() to a byte (or uint8_t).
How much I'll improving RAM on each 'uint8_t' instead 'Int' ?
Thanks to you, I've changed (not all yet) some 'Int' to 'uint8_t'... result... my sketch length on Mega 2560:
Before was ~133000 bytes
Now is ~113000 bytes!!!
Amazing!
Finishing the changes, my next step will be apply 'Progmem' to manipulate my string arrays to LCD.
Thanks once again by your help!
Best regards
Pedro Ferrer
It are the basics of programming explained by the creators of C and UNIX. It will take a while to go through the book but you will learn so much from it ...
If you really want to learn why the executable got smaller you need to learn about compiler building, assembly and that kind of stuff. Not for the faint of heart
With the tool objdump.exe you can see the assembly language generated by the arduino compiler and see what has happened.
[in short, changing datatypes from int16 to int8 size gave the compiler the chance to use other assembly instructons to do the code]
If questions remain, please feel free to ask them.
After replace variables 'Int' 616 times by 'uint8_t' my sketch reduced from 132146 bytes to 113890 bytes using arduino.exe compiler!
Sounds well worth it. Does it still work? I'd be very nervous about the possibility for tricky bugs to arise if you didn't carefully consider the range of expected values for each int you replaced.
wildbill:
Sounds well worth it. Does it still work? I'd be very nervous about the possibility for tricky bugs to arise if you didn't carefully consider the range of expected values for each int you replaced.
Now it works... I didn't changed all the 616 vars... it was just a test to see the sketch dimension, but I can ensure that lot of them were replaced by hand.
I've found some bugs... some vars that have value '-1' and I forgot... I had to undo to 'Int' again.
The sketch is too complex... almost 2 years developing the code... you can look some movies on youtube if you look for 'COADAS+'