Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I wanted to save some values to the EEPROM and also wanted to free up SRAM by avoiding some variable declarations, but EEPROM memory is byte wise.

If I want to store an int value, I have to use some expressions repeatedly. I thought I would make some functions for those. But I am concerned that, if I create a function, it would still occupy SRAM memory, better I declare an int variable instead of using EEPROM.

How are the functions and the local variables stored in SRAM? Does it only store the address of the fuction pointer from the flash memory or all the variables and commands are stored on the stack?

share|improve this question
Do remember that EEPROM is only writable for a limited number of times, reading it is unlimited. According to the AVR datasheet EEPROM only has 100000 cycles, which sounds as a lot but when you try to use it as SRAM, it will only last a fairly short period. –  jippie Jul 4 '14 at 22:17
OMG! After that, will the EEPROM be useless? I am going to check the datasheet! –  Nafis Jul 5 '14 at 9:36
The Flash memory also has a lifecyle. It's wiser to not burn the program a lot. –  Nafis Jul 5 '14 at 9:54
With normal use the numbers given for flash and EEPROM are no problem at all. The equation changes when you start using it like you use SRAM. –  jippie Jul 5 '14 at 12:49

3 Answers 3

up vote 3 down vote accepted

Only the function's data is stored on the stack; its code stays in flash. You can't really reduce SRAM use by using EEPROM instead because, as you have seen, EEPROM isn't addressable in the same way. The code to read and store EEPROM also needs to use some SRAM - probably as much SRAM as you were trying to save! EEPROM is also slow to write, and has a limited lifetime (in number of writes to each byte), both of which make it impractical to use for storing the kind of temporary data we usually put on the stack. It is better suited to saving infrequently changed data, like the unique device configuration for mass-produced devices, or capturing infrequent errors for later analysis.

Edited: There is no stack for that function until the function has been called, so yes, that is when any of the function's data get's put there. What happens after the function returns is that its stack-frame (its reserved area of SRAM) is no longer reserved. It will eventually be re-used by another function call. Here is a diagram of a C stack in memory. When a stack frame is no longer useful, it is simply released and its memory becomes available to be re-used.

share|improve this answer
I am thinking this way, when the function is called, only then the data inside it is stored in the stack. After execution of the function, the data are erased from stack/SRAM. Am I right? –  Nafis Jul 4 '14 at 20:29

Local variables and function parameters are stored on the stack. However, that is not a reason not to use them. Computers are designed to work that way.

Stack memory is only in use while a function is active. As soon as the function returns, the memory is freed. Stack memory is a GOOD Thing.

You don't want to use recursive functions with lots of levels of recursion, or allocate lots of large structures on the stack. Normal use is fine however.

The 6502 stack is only 256 bytes, but the Apple II works just fine.

share|improve this answer
So, you mean the function will be saved with all its local variables, parameters and expressions to the stack temporarily, only when it is called? Otherwise it will stay in the program/flash memory? After execution, will it be erased from stack? I was talking about Arduino actually, as it is Arduino Forum, I didn't mention that. –  Nafis Jul 4 '14 at 20:16
No, only the function's parameters and local variables are on the stack. The code of the function is not saved on the stack. Don't over-think this. –  Duncan C Jul 4 '14 at 20:19

The AVR (the microcontroller family traditionally used on Arduino boards) is a Harvard Architecture, meaning that executable code and variables are in two separate memories - in this case flash and SRAM. The executable code never leaves flash memory.

When you call a function the return address is usually pushed to the stack - the exception is when the function call happens at the end of the calling function. In this case the return address of the function that called the calling function will be used instead - it is already on the stack.
Whether any other data is put on the stack depends on the register pressure in the calling function and in the called function. Registers are the working area of the CPU, the AVR has 32 1-byte registers. The registers can be accessed directly by CPU instructions, whereas data in SRAM will first have to be stored in registers. Only if arguments or local variable are too big or too many to fit in registers will they be put on the stack. However, structures are always stored on the stack.

You can read the details of how the stack is used by the GCC compiler on the AVR platform here: https://gcc.gnu.org/wiki/avr-gcc#Frame_Layout
Read the sections "Frame Layout" and "Calling Convention".

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.