C
chris.ritchie
My question is very basic, but I can't find any material on it.
Compare these:
string x; // only used within the following loop
for (int y = 0; y < 10; y++){
x = toString(y);
use(x);
}
---
for (int y = 0; y < 10; y++){
string x = toString(y);
use(x);
}
shrinking with each iteration? Does the compiler ensure that x is
created just once and the scope is limited? Is 1 the only way to
ensure x is initialized just once?
Not a concern for this case, but with larger objects and longer loops I
worry. Thanks!
Compare these:
string x; // only used within the following loop
for (int y = 0; y < 10; y++){
x = toString(y);
use(x);
}
---
for (int y = 0; y < 10; y++){
string x = toString(y);
use(x);
}
But what about memory management? Does my stack keep growing andFrom a style standpoint, 2 is better because the scope of x is correct.
shrinking with each iteration? Does the compiler ensure that x is
created just once and the scope is limited? Is 1 the only way to
ensure x is initialized just once?
Not a concern for this case, but with larger objects and longer loops I
worry. Thanks!