luxy:
I didn't gave good example. What about this case? Which takes more SRAM? I am wondering how numbers are treated in memory if you don't define them first. Does compiler reserve the same amount of space (4 bytes for float in this case) if I calculate as in Case 1? If I make local constant inside function is also destroyed after function is finished?
Case1:
float result = 3.45 * 6.78;
Case 2:
const float A = 3.45;
const float B = 6.78;
result = A*B;
Generally, if any variable is a constant, you should use the const keyword. This informs the GCC compiler and most often these values will stay in Flash memory. I say most often, because the compiler has a mind of her own and will make a liar out of me if I do not qualify the statement!
But, you can find out easily for yourself! If you are using any of the 1.0.x versions, you can analyze the ELF file created in the temp directory to uncover the memory allocation.
On Windows (any version) the following script I wrote will give you the necessary information:
(Edit the first line if your Arduino installation is not standard or on a 32-bit machine):
PATH=%path%;C:\Program Files (x86)\Arduino_106\hardware\tools\avr\bin;
C:
CD %TEMP%
MD %PUBLIC%\ELFtemp
for /R %TEMP% %%f in (*.elf) do XCOPY /D /Y %%f %PUBLIC%\ELFtemp\
DIR %PUBLIC%\ELFtemp\*.elf /s /b /O:-D /T:W >ElfRhere
SET /P ELF= <ElfRhere
ECHO %ELF% >MemUsage.txt
AVR-SIZE -C %ELF% >>MemUsage.txt
NOTEPAD MemUsage.txt
CD %PUBLIC%\ELFtemp
ECHO y | DEL *.* \%1
SET ELF=""
Just create a Windows script ending with the file extension CMD ... mine is "MemoryUsage.cmd" and set up a shortcut icon.
My last compile looks like:
C:\Users\Public\ELFtemp\GLCD5110_BMP085.cpp.elf
AVR Memory Usage
Device: Unknown
Program: 19932 bytes
(.text + .data + .bootloader)
Data: 2800 bytes
(.data + .bss + .noinit)
OR
If you are brave, use the Arduino GUI 1.6.0rc1 which gives you that info automatically after each compile.
Everything you wanted to know about variable types in GCC is here.
But do not get too hung-up on this because when you move to Due or another uC architecture where the core is not 8-bit, things change!
Ray