Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++11 lambda high-order function wrapper recursive error

#include <iostream>
#include <functional>
using namespace std;

function<int(int)> wrapper(function<void(int)> f)
{
    auto ff = [&](int a) {
        cout << "in wrapper " << a << endl;
        f(a);
        return 1;
    };
    return ff;
}

int main()
{
    auto fa = wrapper([](int a){
        cout << "in fa " << a << endl;
    });
    fa(999);
    wrapper([&fa](int b){
        cout << "in anon " << b << endl;
        fa(998);
    })(997);
}

The above code will print

in wrapper 999
in fa 999
in wrapper 997
in anon 997
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
in wrapper 998                                                                                                                                                  
in anon 998                                                                                                                                                     
..........

until Segmentation fault.

I write the same code in javascript

function  wrapper(f)
{
    var ff = function(a) {
        console.log("in wrapper %s", a);
        f(a);
        return 1;
    };
    return ff;
}

(function ()
{
    var fa = wrapper(function(a){
        console.log("in fa %s", a);
    });
    fa(999);
    wrapper(function(b){
        console.log("in anon %s", b);
        fa(998);
    })(997);
})();

It will print

in wrapper 999
in fa 999
in wrapper 997
in anon 997
in wrapper 998
in fa 998

What's with the c++ lambda code? I tried the g++-5.0 and g++4.9 compiler. All ended up with this error.

like image 333
Naryc Avatar asked May 31 '26 01:05

Naryc


1 Answers

ff captures f by reference, but f is local to wrapper. When you return ff, that reference becomes dangling, and undefined behaviour is triggered upon calling ff.

like image 147
Quentin Avatar answered Jun 01 '26 15:06

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!