How Arduino Manages memory?

I played around with arduino for some years and I'm wondering how Arduino uses it's memory, I mean, When you program this: int potato; is the same than: int potato = 0; or #define potato 0 How does arduino store those values? Writes the position of the variable in the FLASH and then the value in RAM or how it does?

The first two are stored as 2 bytes (which is what an int is) of SRAM, 0x0000. Your sketch can assign potato a new value from -32786 to 32767. This is often abused, especially for pin assignments, and byte would be sufficient, with a value from 0 to 255, or 0xff.
The third tells the compiler "every time you see 'potato' put a 0 in the code", so that ends up in flash as part of the code. Your sketch can not assign potato a new value.
const int potato = 0; is similar and seems to be preferred to
#define potato 0
with some other code implication that I don't know how to explain. I rarely use either myself, but I don't write sketches that are tremendously huge either.

CrossRoads:
The first two are stored as 2 bytes (which is what an int is) of SRAM, 0x0000.

For the first one, that depends if it's a global variable or a local variable :wink: If it's a global variable, yes; if it's a local variable, it can have any (initial) value.

None of your questions have any thing to do with the Arduino! They are all to do with C/C++.

Mark

I found the following link to be helpful to me back when I was looking into something similiar to your question. Don't know if it will be any interest to you but worth a look.

Scroll down to the 3rd post I believe. It shows the allocated memory taken by each variable and possible value for each type of variable.

Good luck,

  • Matt

#define works a lot like find/replace; you can do things with it that would not be valid with const variables (which also means you can accidentally do weird stuff with them too) - whereas const variables work just like normal variables except you can't change them.