Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I capture a local variable by reference, and it goes out of scope?

Tags:

c++

c++11

lambda

Suppose I use a lambda as a callback function, and when creating the lambda, I capture a local function variable by reference. Now suppose that the lambda object does not get executed until after that local function variable goes out of scope. What happens?

I realize that it would be pretty stupid for someone to do so if there's a chance of it happening, but I am almost positive that someone would end up doing it.

like image 533
s73v3r Avatar asked Feb 28 '12 19:02

s73v3r


People also ask

What happens when a variable of reference data type goes out of scope?

Nothing physical happens. A typical implementation will allocate enough space in the program stack to store all variables at the deepest level of block nesting in the current function. This space is typically allocated in the stack in one shot at the function startup and released back at the function exit.

What does it mean for a variable to go out of scope?

In C++, variables are declared with their types, and variables declared within an if-statement block (or any other block) are also local to that block. This means that they go out of scope when the block ends. If we were to try and use them outside of the block in which they were declared, an error would occur.

What happens when a variable goes out of scope C++?

The cases when object goes out of scope, The program goes out of the scope of a function. The program ends. The block initializing local variables of object goes out of scope.

How do you create a lambda function in C++?

Creating a Lambda Expression in C++auto greet = []() { // lambda function body }; Here, [] is called the lambda introducer which denotes the start of the lambda expression. () is called the parameter list which is similar to the () operator of a normal function.


2 Answers

Yes, that would be following a dangling reference. It sounds like you're worried about interface design: "I am almost positive that someone would end up doing it." Please don't reject lambdas and std::function on this basis, as they are no more dangerous than any other alternative. Lambdas are just a simpler way to define local functors. std::function is the best interface to persistent, polymorphic functors, lambda or not.

The scope issue is why it's easier to capture by value. The user won't get a reference unless they write &. Of course, the danger is that someone would get in the habit of starting all their lambda functions with [&], since references are "faster." Hopefully any such person would learn their lesson soon enough… although some pointer-happy folks are just incorrigible.

like image 188
Potatoswatter Avatar answered Sep 28 '22 18:09

Potatoswatter


The same thing that happens when you return a reference to a local variable: undefined behavior.

like image 23
Nicol Bolas Avatar answered Sep 28 '22 20:09

Nicol Bolas