Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of a variable initialized in the parameter list of a function

The following code builds, compiles and runs (C++, mingw) seemingly without any problems. However, am I guaranteed that objects constructed with initializer-lists inside a function's parameter list, lives through the scope of that function, even though the function takes the argument by reference?

If not, is it true that when creating an object using its initializer-list in a function's parameter list (which takes the argument by reference) may be dangerous because it will immediately be destructed: In this case, the function doesn't have a copy, but a reference to memory which may, or may not be reallocated by another process?

struct S {   S() : a(0), b(0) {}   S(int a, int b) : a(a), b(b) {}   int a;   int b; };  void foo(const S& s) {   std::cout << "s.a = " << s.a << std::endl;   std::cout << "s.b = " << s.b << std::endl; }  int main() {   foo({4,5}); // <-- What is the scope of the struct initialized here?    return 0; } 
like image 257
Wololo Avatar asked Feb 14 '18 11:02

Wololo


People also ask

What is the scope of the parameter in a function?

A scope is a region of the program and broadly speaking there are three places, where variables can be declared: Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters.

What is the default scope of variable that defined inside a function?

Any variable used inside a function is by default limited to the local function scope.

What is the scope of a variable in C++?

Scope of Variables in C++ In general, the scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with.

What do you mean by scope of variable explain with example?

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions.

What is the scope of a function parameter?

Scope of function parameters. Local variables have their scope to the function only in which they are declared, while global variables have scope to the program and they can be accessed by any function in the program. Look at following function: Here, x and y are the function parameters and sum is the local variable.

Which variables have local scope in C++?

Parameters to a function call have local scope unless they are passed by reference ( then also the change is visible to calling function only which is not true definition of global scope). So the variables defined inside the functions or as parameters to the function have local scope.

What is the scope of a variable?

In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with. There are mainly two types of variable scopes: Now let’s understand each of the scope at a greater detail:

How many types of variable scopes are there in Python?

There are mainly two types of variable scopes: Now let’s understand each of the scope at a greater detail: Variables defined within a function or block are said to be local to those functions.


1 Answers

According to cppreference [lifetime]:

All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created, and if multiple temporary objects were created, they are destroyed in the order opposite to the order of creation. This is true even if that evaluation ends in throwing an exception.

That means that the temporary object will be destroyed after the function has returned, so it's perfectly safe.

like image 74
amc176 Avatar answered Sep 30 '22 17:09

amc176