'uint8_t' instead 'Int'

Good afternoon

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 would like to know if I'm improving something using 'uint8_t' instead 'Int'..

Yes, quicker and consuming less program and RAM memory.

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.

Hello

Thanks by your prompt answers.

How much I'll improving RAM on each 'uint8_t' instead 'Int' ?

(...)

PaulS
Eg: using ITOA... Integer to Ascii... is some of the reason that's why I'm using 'Int' instead 'Byte'

Thanks once again
Best regards
Pedro Ferrer

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' ?

50%. A uint8_t is one byte. An int is two bytes.

Hello

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

Just to clarify, the RAM usage does not directly affect sketch length.

As an example:

byte foo [1000]; // takes 1000 bytes of RAM
int bar [1000];  // takes 2000 bytes of RAM

Whilst the compiler generates more code to access "bar" than "foo" it will be a few bytes more (of sketch size) not 1000 bytes more.

Good morning

Hum...
Something happened, because beside change 'Int' to 'uint8_t', I've applied some 'const uint8_t' instead 'Int'... and get ~20000 bytes less...

Best regards
Pedro Ferrer

Yes, no doubt.

I would like to optimize Arduino RAM utilization.

...

my sketch length on Mega 2560:
Before was ~133000 bytes
Now is ~113000 bytes!!!

The sketch length is nothing to do with RAM. It is program memory size, which is a totally different part of the processor.

Hello

Ok... more details...

After replace variables 'Int' 616 times by 'uint8_t' my sketch reduced from 132146 bytes to 113890 bytes using arduino.exe compiler!

This is what I get.

Please let me know
Thanks on advance
Best regards
Pedro Ferrer

Did any of those 616 numbers ever need to count above 255 or are they ever negative, if so you may run into problems.

Let you know what? You didn't ask a question.

Hello

Arrch:
Let you know what? You didn't ask a question.

Please let me know... because I don't understand why the sketch was reduced in so many bytes...
Thanks on advance
Best regards
Pedro Ferrer

read this book - http://letrongngoc.tech.officelive.com/Documents/TheCProgrammingLanguageSecondEdition.pdf - especially the part about sizeof, better read the whole book.

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 :slight_smile:
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.

Hello

Thanks by the tip!
I'll read!

Thanks once again
Best regards
Pedro Ferrer

This is because, as AWOL said, the int type "will require more program memory to hold the extra instructions to fetch and manipulate it" than uint8_t.

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.

After replace variables 'Int' 616 times by 'uint8_t' my sketch reduced from 132146 bytes to 113890 bytes using arduino.exe compiler!

I was wondering why a sketch uses 616 variables?
This sounds like room for functions and arrays and refactoring

Can you post the (new) code?

Hello

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+'

Thanks
Best regards
Pedro Ferrer