0

When I declare some variables inside a function and then call that function inside a loop, are the variables destroyed and reconstructed at each step? Is that bad and can/should be avoided? Here an example of what I'm doing:

    void myfun(vector<double> &x){
         vector<double>y;
         y.resize(x.size());
         //computation
    }

    int main(){
        vector<double>x(3,0);
        for(int i=0;i<5000;i++)
        myfun(x);
    }  
3
  • Locally declared variables are destroyed after they lose scope-that is the minute your function ends. Commented Feb 9, 2012 at 15:58
  • As I thought. But for large i, is preferable to avoid that? Commented Feb 9, 2012 at 16:00
  • 1
    no you shouldnt avoid it - it isn't a bad thing. I dont care if i is large or not. Commented Feb 9, 2012 at 16:02

6 Answers 6

1

Yes they are destroyed. It is not bad.

Sign up to request clarification or add additional context in comments.

Comments

1

If you put the static qualifier on the variable in your function then it wouldn't be destroyed every time. Otherwise, it will be, but that is not necessarily bad. Refrain from doing optimizations for speed until you actually know there is a problem and know what is causing it.

1 Comment

Exactly. Making the vector static may seem to do no harm, but it will likely cause headaches down the road if he decides to make his code multithreaded.
0

Depends on what you are doing with it, sometimes it leaves no choice but to do just that. What I mean to say is that, if its kind of reusable, you can have that "inside-declared" variable declared outside and pass that too as a reference argument to the function.

2 Comments

by doing this you are avoiding the redeclaration at each step. Another way of reuse might be putting the static modifier, that is only one copy throughout all the calls.
that's what I was looking for, couldn't think of. Thanks
0

When I declare some variables inside a function and then call that function inside a loop, are the variables destroyed and reconstructed at each step?

Functions can be inlined by your compiler, variables optimized away and all sort of unpredictable stuff, but you can assume that they are initialized and destroyed at each function call.

Is that bad and can/should be avoided?

Most of the time it is unavoidable, but you must always try to minimize this when dealing with vectors if possible.

Comments

0

C++ standard provides something called an "as-if" rule, which basically says that the compiler can do anything it wants with the code, provided that you cannot tell the difference. If the compiler observes that skipping constructor and destructor calls does not change the behavior of the program, it may skip it, or declare the variable outside of the function, or not declare it at all, if it does not change the behavior of the program. It depends on the quality of implementation. Although I am not sure if any compiler applies such optimizations.

Comments

0

Non-static local variables are destroyed when your function terminates. You cannot avoid it. If you want to avoid creating them each time you enter the function, you can either declare them static or just pass them as an argument (by reference or pointer).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.