Home > Back-end >  Does a object stores lambda function have it's own address?
Does a object stores lambda function have it's own address?

Time:11-08

Based on the result of this topic

A lambda which captures no variables (nothing inside the []'s) can be converted into a function pointer

I have written a program like

void test1(){}
int main() {
    auto test2 = [](){};
    printf("%p\n%p\n", test1, &test1);
    printf("%p\n%p", test2, &test2);

    return 0;
}

the result are

0x561cac7d91a9

0x561cac7d91a9

0x7ffe9e565397

(nil)

on https://www.programiz.com/cpp-programming/online-compiler/

So the test2 is storing a function pointer to the lambda function?

and my question is that object test2, which stores the lambda data, not have its own address?

I thought this test2 should have its own address.

CodePudding user response:

Does a object stores lambda function have it's own address?

Yes, like all objects in C the variable test2 also has a unique address. You can see this by printing &test uisng cout as shown below:

int main() {
    auto test2 = [](){};
     
    std::cout << &test2;  //prints address of test2
    return 0;
}

Demo

The output of the above modified program is:

0x7ffe4d4b5fcf
  • Related