As I been learning Arduino (micro controllers, c++, etc) I have learned that memory is very limited. I already understand how memory works when it comes to variables. But I do not understand how memory works when it comes to functions. Since I am a backed developer I try to stay away from interrupts and instead use callback functions. Another reason why I prefer to use callback functions is because I love the visual studio IDE. I create my core functionality of whatever project I am building on a c++ project targeted for Windows. Once my algorithms/logic works the way I want it I reference those libraries on Arduino.

Anyways I am using lambdas to pass functions to my helper libraries. That way I do not use the delay function to wait for a response and I am always listening for events. Basically my question is if it is correct to have code like this (function SendEvent):

/* MyRadioLibrary.h */
// I am not including all the code of `MyRadioLibrary` because that makes it hard to 
// understand what I am asking. Instead I will try to use a lot of pseudo-code. Let me 
// know if you want me to post the code as an edit.

class MyRadioLibrary


    static void SendEvent(int typeOfEvent, void(*onEventCompleted)(bool x))
        // I have an array of pointers to functions 
        void(*onEventCompletedBuffer[10])(bool x);

        // I enqueue that event with a queue helper class 

        // Send event ....


    static void loop() {

         // listen for radio messages
                // get response
                // dequeue pointer to call back   
                // execute call back

And my arduino sketch contains this code:

#include <MyRadioLibrary.h> 

void setup()  {
    // setup pins etc..

void loop()

    if (isButton1Pressed())
        MyRadioLibrary::SendEvent(1, [](bool didEventCompletedSuccessfully) {
            if (didEventCompletedSuccessfully)
                // turn on green LED
            else {

                // turn on red led

    else if (isButton2Pressed())
        MyRadioLibrary::SendEvent(2, [](bool didEventCompletedSuccessfully) {
             // call back function

    MyRadioLibrary::loop(); // listen for response events. When we get one fire call back.

Now the question

When I call the SendEvent function I am passing 2 things (1) an integer and (2) a pointer to a function that is the call back. The integer (1) is passed as value and that will be part of the stack of the SendEvent function. The pointer (2) will be stored on an array of pointers. But what about the lambda function? where will that be stored? Instead of using a lambda function should I declare those functions in global scope and pass the global function instead of a lambda? Will I run the risk of the pointer (2) pointing to a function that no longer exists if I use this way of coding?

  • Re “pass the global function instead of a lambda”: Once it's translated into machine code, it makes no difference whether it's a lambda or a regular function. Jul 27, 2020 at 20:08

1 Answer 1


Your functions won't cease to exist. All of the code in your compiled and linked code image gets loaded into code-memory (Flash) and it stays there throughout the run, through resets, and through power-cycles, until another image is loaded over it.

Are you confusing pointers to functions with pointers to a function's local variables which occupy RAM memory, specifically within the function's stack-frame which is transient, being created and "destroyed" as the function gets called and returns - and successive stack-frames won't necessarily be created in the same place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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