As far as I know, declaring a variable inside a loop is less efficient than declaring outside and modify it inside the loop.
Example:
std::list<<double> l;
for(int i = 0; i < 100000; ++i)
{
double a;
a = 500.0 * i;
l.append(a);
}
Another example with pointer:
std::list<<double *> l;
for(int i = 0; i < 100000; ++i)
{
double* a;
*a = 500.0 * i;
l.append(a);
}
The examples don't have enough sense but I just want to show that the double and the pointer are being declared inside the loop.
The thing is, the scope of the variable is the same as the loop, so when the loop do an iteration, will it destroy the variable and then declaring it again? Or it just stays in the heap until the end of the for
loop? How efficient is it to do that? Is it a waste of resources?
I code it as it was in C++.
Thanks in advance!