There has been asked that question already. The answer was "Stack space for local variables is usually allocated in function scope." and so there is no difference in overhead in declaring variables outside/inside of a loop.
Now, imagine that we have a snippet with a function inside of the loop:
void do_sth(int &i) {int var=i+1;}
int i = 0;
while(i < 100)
{
do_sth(i);
i++;
}
And a second snippet with a variable declared outside:
int i = 0;
int var;
while(i < 100)
{
var = i+1;
i++;
}
My question is - what is the overhead in case of the first snippet in a practical scenario (with a modern compiler)? If indeed there is an overhead, then how big is it? Is it comparable with, say, doing extra addition (operator+) on integers in each step of the loop?