2

i just wanted to know what is the best way to pass a local variable to a function.

void check2 (int* var2){
    *var2=7;
}

void check1 (int& var){
    var=6;
}

int main()
{
    int var;
    int* var2=new int;

    check1(var);
    check2(var2);

    delete var2;

    return 0;
}

In check1, I pass the variable using a reference. As I am passing a local variable to the function check1, wouldn't it get out of scope once main terminates and there would be no variable anymore?

I found a couple of examples where the operator new is used to allocate memory and return a pointer which is then passed to the function. Is this a better way to do the same task as the variable doesn't get erased?

Patryk
  • 22,602
  • 44
  • 128
  • 244
Alex Gors
  • 153
  • 1
  • 2
  • 7
  • There is no best way. With pointer syntax you make it more clear that the variable is passed by reference. With reference syntax the code is cleaner – Serve Laurijssen Jun 20 '16 at 12:44
  • 1
    Once main terminates, every variable in your program gets destroyed – ForceBru Jun 20 '16 at 12:44
  • related/kinda dupe: http://stackoverflow.com/questions/3613065/when-to-pass-by-reference-and-when-to-pass-by-pointer-in-c – NathanOliver Jun 20 '16 at 12:45
  • 4
    Using `new` or not has nothing to do with passing variables to functions. – juanchopanza Jun 20 '16 at 12:46
  • 1
    No! Don't do unnecessary dynamic allocation! No naked owning pointers! –  Jun 20 '16 at 12:46
  • Best way in relation to what? Code optimization, readability,.... And what is it you are trying to achieve, faster code, cleaner code? – thorsan Jun 20 '16 at 12:47
  • I hope you realize that you can also do check2(&var); and that it does not matter much if you use pointer or reference since the compiler will optimize/inline it anyway and produce the exact same result. Fiddling with "new" however is a different story, it is much slower than using variables on the stack. – Sven Nilsson Jun 20 '16 at 12:48
  • 1
    Why are you worrying about using a variable after main has gone out of scope.What could you possibly do after main has gone out of scope? – Gaurav Sehgal Jun 20 '16 at 12:49
  • @GauravSehgal : Completely unrelated to original post, but.. after main goes out of scope, you might still have static destructors to be executed. – lorro Jun 20 '16 at 14:27

1 Answers1

2

[What is the] Best way to pass local variable to function

Depends on why you pass the variable, and what you do with it.

Passing a reference implies that the function does not take ownership of the object, and the function clearly doesn't take ownership, so that's appropriate. Passing a bare pointer is ambiguous about change of ownership so in this case, reference is better.

As I am passing a local variable to the function check1, wouldn't it get out of scope once main terminates and there would be no variable anymore?

Correct. However, whether the object exists after main returns, is mostly orthogonal to the question of how to pass the object to a function within main.

Also do realize that after main returns, the whole program is about to terminate. The only situation where you'd still need an object to exist at that point, is if that object is depended on by a destructor of another object that has static storage.

I found a couple of examples where the operator new is used to allocate memory and return a pointer which is then passed to the function. Is this a better way to do the same task as the variable doesn't get erased?

If you do need to create an object in main, but need that object to exist after main has finished, then dynamic allocation is one way to achieve that. Static storage might be an alternative.

In this example however, you delete the object in main, so it gets destroyed just like the local variable does, so in this case, the dynamic allocation offers no advantage.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • "but need that object to exist after main has finished".Can you please provide a scenario where this would be needed. – Gaurav Sehgal Jun 20 '16 at 12:56
  • 2
    *but need that object to exist after main has finished* How can something outlive `main`? `main` ends the program which ends all of the lifetimes. – NathanOliver Jun 20 '16 at 12:56
  • 4
    @NathanOliver, not true. Static objects outlive main. (Think destructors). – SergeyA Jun 20 '16 at 12:59
  • 3
    Downvoting this answer is unjust. Answer is correct. – SergeyA Jun 20 '16 at 12:59
  • 2
    @NathanOliver After `main` returns, the program doesn't end until the destructors of static objects have run. Presumably one of static objects may refer to an object created in `main` and the destructor can depend on the referred object. – eerorika Jun 20 '16 at 13:01
  • @GauravSehgal I have no practical example. Ask Alex Gors, who is concerned that their object is destroyed at the end of `main`. – eerorika Jun 20 '16 at 13:02
  • @user2079303. Thanks for the explanation. – Gaurav Sehgal Jun 20 '16 at 13:06
  • 1
    Practical case, for example, is setting Structured Exception translator in constructor before entering `main`. – lapk Jun 20 '16 at 13:07
  • 2
    The destructors for standard streams (`std::cout`, `std::cerr`, `std::cin`, etc) are invoked after `main()` returns - in fact, after the destructors of all (user-defined) static objects. That's one reason those streams can be safely used in destructors of (user-defined) static objects. Among other things, the destructor for an output stream (like `std::cout`) flushes buffers before the object's life ends. – Peter Jun 20 '16 at 13:14
  • @SergeyA Yeah I was missing that. Thanks for reminding me. – NathanOliver Jun 20 '16 at 13:22