Variables scope/memory management

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris.ritchie@gmail.com

    #1

    Variables scope/memory management

    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!

  • Marcus Kwok

    #2
    Re: Variables scope/memory management

    chris.ritchie@g mail.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

    Comment

    • Gavin Deane

      #3
      Re: Variables scope/memory management


      chris.ritchie@g mail.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

      Comment

      Working...