Variables scope/memory management

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);
}

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!
 
M

Marcus Kwok

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);
}


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.
 
G

Gavin Deane

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);
}


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top