TransWikia.com

Is it required to delete variables before going to sleep?

Arduino Asked by Tono Nam on November 30, 2021

This tutorial shows how to put the arduino to sleep and use a watchdog to wake up at intervals:

https://youtu.be/urLSDi7SD8M

I been studding c++ and as I am getting better I am instantiating objects with the new keyword. I have learned that if you create something with the new keyword you are responsible for deleting it from memory when you no longer needed. But I do not know if this applies when you put arduino to sleep.

Anyways my question is in the comments of the code:

Foo * someObject;


void setup()
{
    someObject = new Foo(arg1, arg2, ..); // crete an instance of someObject
   // init pins etc..
}

void loop()
{
     if(button1Pressed())
     {
          
          // **** DO I HAVE TO DELETE OBJECTS BEFORE GOING TO SLEEP? Do I need this line? ****
          delete someObject; 

          gotToSleepAndWakeUpIn4Seconds() ;  // places arduino in sleep mode and wakes up again in 4 seconds
          
          int x = 0; // this line never executes arduino is on sleep mode and will wake up again in 4 seconds
     }

     // more code
     if(something)
        someObject->executeFunction1();

     // etc...
     // ....
}

One Answer

You have probably learnt that objects created with new are stored in the heap, while globals are stored in the .data and .bss sections, and locals on the stack¹. This distinction, however, only exists in the software. At the hardware level, .data, .bss, heap and stack are just arbitrary portions of the RAM.

Think of what would happen if the RAM was erased, or somehow corrupted, when the microcontroller sleeps. Not only would that affect the heap-based objects, but also every variable in the program. And even the return address from the call to gotToSleepAndWakeUpIn4Seconds() would be lost, so the CPU would not know where to resume execution when it wakes up. The only reasonable thing to do at wakeup would then be to restart the program from scratch. And this is actually what happens when you reset the microcontroller².

But a sleep is not a reset. The contents of the RAM is preserved while sleeping. This means that the whole state of the program is preserved: locals, globals and heap-based objects. No need to delete and re-create stuff.

As a side note, if you can allocate memory statically, this is usually better than dynamic allocation, ans you avoid the risk of heap fragmentation:

Foo someObject(arg1, arg2, ..);  // static global instance

void loop()
{
    // Some code, then,

    if (something)
        someObject.executeFunction1();
}

Notes:
¹ As an optimization, locals of the current stack frame may be in CPU registers, but this doesn't change the conclusion.
² On AVR, a warm reset doesn't loose the RAM, but it is then reinitialized by the C runtime. A cold reset (power-cycle) does loose the RAM.

Answered by Edgar Bonet on November 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP