1

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...
     // ....
}
  • 3
    No you don’t. You just have to free it before you try to use that memory for anything else. Really on a microcontroller I would create the object in a smaller scope and try to do everything in my power to avoid having to use new or any other form of dynamic allocation. There are usually better ways around it in this kind of environment. Very different from writing code to run on a PC. – Delta_G Jul 22 at 17:58
5

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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