disclaimer
I'm a programming n00b so I may not know what I'm talking about.
/disclaimer

My question boils down to scopes.
Let say I have code

{ 
string mystring1;
string mystring2;
string mystring3;
 
//code dealing with mystring1
 
//code dealing with mystring2
 
//code dealing with mystring3
}

versus

{
string mystring1;
//code dealing with mystring1
}
 
{
string mystring2;
//code dealing with mystring2
}
 
{
string mystring3;
//code dealing with mystring3
}

Now my guess is that the first block of code uses more memory at any given moment than the second one, and that the second block of code uses more than or equal processing power than the first one.

Are these assumptions true? Which one would you use? And each mystring only has scope within the {} enclosing it right?

I ask this because I have some code that initializes like 10 strings at once (like block 1) and then works with them one by one disjointly from eachother. And when I run my program, it lags a second during the initialization of these strings. So I figured I could rewrite it like block 2 to make it run smoother.

Recommended Answers

All 3 Replies

I think if possible you should go as local as you can keeping in mind memory.

As far as processing power is concerned, you should'nt care unless you are working on a system having limited amount of processing power. You can also profile your code to check.
I don't have any exact answer for this. Maybe someone else will through some more light on it.

>>Now my guess is that the first block of code uses more memory at any given moment than the second one
In the second example, a good optimizing compiler will notice that it only needs to allocate space for one std::string object and reuse this memory on each block entry. All memory is allocated on the stack during function entry, not block entry. That makes the second example more memory efficient than the first. It would be the same as declaring one std::string object at the beginning of the function and re-using it in each block, something like this: (in this example you don't need the blocks at all!)

int foo()
{
  std::string str;
  {
     str = "Hello";
    // blabla
  }
  {
     str = "By";
    // blabla
  }
}

>>And each mystring only has scope within the {} enclosing it right?
That is correct -- but it doesn't mean that the memory allocated for the objects are removed from the stack when the block terminates. It is only released when the function returns to whoever called it.

>>the second block of code uses more than or equal processing power than the first one
Depends on how you look at it. Overall, from the beginning to the end of the function they are both equal. But the second will consume less initial processing than the first because std::string class constructor will be called for all three strings at the same time in the first example, while the constructors are called only duirng block entry in the second example.

Good explaination, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.