By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

Variables scope/memory management

P: n/a
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);
}

>From a style standpoint, 2 is better because the scope of x is correct.
But what about memory management? Does my stack keep growing and
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!

Sep 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ch***********@gmail.com wrote:
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);
}

>>From a style standpoint, 2 is better because the scope of x is correct.
But what about memory management? Does my stack keep growing and
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?
I would tend to use 2 since it restricts the scope of x better.

The only way to tell which is better performance-wise is to test it. It
depends on how your std::string is implemented: it is possible that
creating and destroying a string on every iteration could be faster than
assigning on every iteration.

I'm pretty sure I read a GOTW article on this but I can't seem to find
it.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 18 '06 #2

P: n/a

ch***********@gmail.com wrote:
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);
}

From a style standpoint, 2 is better because the scope of x is correct.
But what about memory management? Does my stack keep growing and
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?
The first style is the only way to guarantee x is initialised just
once. I am not sure how much optimisation the compiler is allowed to do
in the case of the second style. However ...
Not a concern for this case, but with larger objects and longer loops I
worry. Thanks!
Never worry about that sort of thing. Worrying won't lead you to a
logical conclusion. Only profiling will do that. Always start by
writing the code in the clearest, least surprising way. If, and only
if, you identify through profiling that constructing and destructing
the object on every loop iteration is an unacceptable performance
bottleneck, then consult your compiler's documentation to see if an
optimisation is available, or resort to your first style.

Gavin Deane

Sep 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.