Is sketch compiled/executed in SRAM or Flash memory?

Hello,

I'm using the Arduino UNO R3 and i learned about the importance of using data types that would not take unnecessary memory and the bad practice of using float type for all numbers as it takes more bytes of memory, etc. But what kind of memory is it using in this case, SRAM or Flash?

From my understanding, when the program is compiled and uploaded, it is sent to the 32 Kb Flash memory of the UNO but when the program is running on the UNO, then the 2 Kb SRAM is utilised. Am i correct?

1. Let me give you the answers to your questions:
(1) Variables of the sketch are stored in SRAM and they remain in SRAM during the execution phase of the sketch. If desired, some of the unchangeable variables could be stored in flash.

(2) The sketch is compiled in the RAM area of the PC; after compilation, the binary codes of the sketch is saved as a file in a folder of the PC. During uploading, the binary codes of the saved file enter into the flash of the MCU.

(3) After pressing of the RST button of UNO, the sketch begins execution from the setup() function during which the variables are initialized in the SRAM area of MCU. These variables are later on accessed and modified as needed by the program codes.

2. As a refeence -- within the ATmega328P MCU of the Arduino UNO Board, there are thee types of storage spaces (FLASH, EEPROM, and SRAM) along with many other hardware resources (Fig-1).


Figure-1: Block diagram showing the hardware resources of ATmega328P MCU

sorry, but the tiitle is stupid. can you correct it?

the flash is not written at runtime but the variables need to change the value fast

GolamMostafa:
(3) After pressing of the RST button of UNO, the sketch begins execution from the setup() function during which the variables are initialized in the SRAM area of MCU.

Nonsense. Variables are initialised during crt0, which runs before even main() is run.
setup() isn't called until after init(), and both are called by main()

TheMemberFormerlyKnownAsAWOL:
Nonsense. Variables are initialised during crt0, which runs before even main() is run.
setup() isn't called until after init(), and both are called by main()

Extremely sorry! Those what you have mentioned do exist -- no question about it; but, as a user I don't see those. I see in my IDE only two functions: setup() and loop(). It is possible to execute a sketch without loop() function; but, it is impossible without setup(). Therefore, the sketch must begin its execution from the setup().

Trouble isn't that you say the Arduino starts execution from setup(), that's true from a user perspective. But that's NOT where variables are initialized in the SRAM :wink:

septillion:
Trouble isn't that you say the Arduino starts execution from setup(), that's true from a user perspective. But that's NOT where variables are initialized in the SRAM :wink:

It is too hard to convince the fresh pupils that there are so many things that have gone in the background. They want to touch what they see.

If I have few variables in the setup() function with initial values, are they not initialized in SRAM as they are encountered line-by-line/instruction-by-instruction?

It is possible to execute a sketch without loop() function; but, it is impossible without setup().

Really ?

Try this :

int main(void)
{
  Serial.begin(115200);
  Serial.println("Hello from main()");
  Serial.flush();
}

GolamMostafa:
It is too hard to convince the fresh pupils that there are so many things that have gone in the background. They want to touch what they see.

It's your job to convince them, no matter how hard it may be to believe.

It's no good tell them fairy tales.

TheMemberFormerlyKnownAsAWOL:
It's no good tell them fairy tales.

When education comes, many a people start with fairy tales; but for technology, every body starts with what it is.

UKHeliBob:
Really ?

Try this :

int main(void)

{
 Serial.begin(115200);
 Serial.println("Hello from main()");
 Serial.flush();
}

The above one is not Arduino friendly?

Try this: (gives output)

void setup()
{
  Serial.begin(9600);
  while (1)
  {
    Serial.println("Arduino");
    delay(1000);
  }
}

Also try this one: (does not even compile)

void loop()
{
  Serial.begin(9600);
  while (1)
  {
    Serial.println("Arduino");
    delay(1000);
  }
}

Also try this one: (gives output)

void setup()
{
  
}

void loop()
{
  Serial.begin(9600);
  while (1)
  {
    Serial.println("Arduino");
    delay(1000);
  }
}

The above one is not Arduino friendly?

For me it compiles, runs and prints the message to the Serial monitor on a Nano using IDE 1.8.10

Note that there is no setup() and loop() function in the program so is your

it is impossible without setup().

correct ?

UKHeliBob:
For me it compiles, runs and prints the message to the Serial monitor on a Nano using IDE 1.8.10

Note that there is no setup() and loop() function in the program so is your

correct ?

Yes! It is correct -- but pseudo correct as the Arduino users have the settled mind set of dealing with setup() and loop() functions and not the main() function which is called upon by the OS in PC Platform. If I use it in the Arduino and makes a return, where will it return?

It is important to know that variables are initialized and objects created BEFORE setup(). That is why you can't use Serial.print() in a class constructor.

If you are not teaching this to your students before you teach them how to make their own classes then I will get more paid work in the future.

MorganS:
It is important to know that variables are initialized and objects created BEFORE setup(). That is why you can't use Serial.print() in a class constructor.

If you are not teaching this to your students before you teach them how to make their own classes then I will get more paid work in the future.

It depends on -- am I teaching Programming Language or Arduino Programming?

We know that the 80x86 initializes a 'hell of data structures' in 'Real Mode' before switching over to PVAM Mode. Should the focus be on PVAM Mode Operation or on the mechanism of initialization or both? Time allocation and course outline are also two important factors.

If I use it in the Arduino and makes a return, where will it return?

I don't know, but when you write a normal Arduino sketch with a setup() and loop() function the IDE actually compiles a main() function into the program behind the scenes

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

Where does that return to I wonder ?