The lifetime of the C++ lambda function?

Asked 2 years ago, Updated 2 years ago, 90 views

#include <iostream>
#include <string>




using std::cout;
using std::cin;
using std::endl;


decltype(auto) getFunc()
{
    return [](int n1, int n2) { return n1 + n2; };
}


decltype(auto) getFuncPtr()
{
    auto func = [](int n1, int n2) { return n1 + n2; };
    return &func;
}


int main()
{
    auto func = getFunc();

    cout << func(1, 3) << endl;


    auto funcPtr = getFuncPtr();

    cout << (*funcPtr)(2, 4) << endl;


    return 0;
}


If you compile and run the example above in Visual Studio 2015, you'll get good results.
But my question is, If the getFuncPtr function returns a normal local variable (such as intx...) It will probably cause runtime errors or bugs.
If you return the address of the local lambda (..?) and use it well, which has (probably) disappeared I thought Lambda would be specialized I'm asking because I want to know the deeper part of C++ Lambda.

Why does the example above work?
When does lambda disappear?

c++ lambda

2022-09-21 21:51

2 Answers

Returns the reference of regional variables that are normally assigned to a stack can cause a crash because they may have unexpected values when using or initializing the stack area, such as other functions. Of course, if a function or program is simple and doesn't cover the area with other data, it may seem like a working " thing."

In order to know exactly how it works internally, disassembling the binary after compiling is the most accurate, so I compiled the code you provided and looked into it. I don't have a VS2015 environment, so I just compiled it from OSX to clang++, and personally, I don't know exactly how the lambda of C++ are handled by the compiler, but I could see some strange results. (It also changes a lot depending on the compiler optimization option.)

First of all, 1 is part of the disassembly of the main function. As you can see, there is a function (2). main

This function (2) receives three total factors (substantially one pointer that acts as two + this) and returns two actual factors that came in. As you can see, this guy (ebp+arg_0) who came over to this is funcPtr, which is the return of the getFuncPtr() call in the main. But it's not being used at all. (That's why even if the data on the stack is overwritten, it doesn't crash.)) lambda

In other words, in the example you uploaded (although you can analyze more accurately if you attach the compiled binary), at least it doesn't crash because it's executing the emited code as a real function, not calling the function pointer. But bugs are bugs.


2022-09-21 21:51

Wouldn't the example above seem to be working temporarily because the actual memory hasn't been deleted yet?

Lambda objects have a functional life equivalent to std::function<> because they are returned as std::function<>. So it's just the life of a normal object.


2022-09-21 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.