what is better? straightforward question on variable declaration

Discussion in 'C++' started by Vasileios Zografos, Jul 1, 2003.

  1. Ok, easy question.
    Not caring about the variable scope what is better (i.e. possibly in
    memory allocation etc)


    int someVar=0;

    for (int i=0;i<1000;i++)
    {
    ...
    someVar++;
    ...
    }


    OR

    for (int i=0;i<1000;i++)
    {
    ...
    int someVar=0;
    someVar++;
    ...
    }




    Or it really doesnt make any difference at all?
    Vasileios Zografos, Jul 1, 2003
    #1
    1. Advertising

  2. "Vasileios Zografos" <> wrote...
    > Ok, easy question.
    > Not caring about the variable scope what is better (i.e. possibly in
    > memory allocation etc)
    >
    >
    > int someVar=0;
    >
    > for (int i=0;i<1000;i++)
    > {
    > ...
    > someVar++;
    > ...
    > }
    >
    >
    > OR
    >
    > for (int i=0;i<1000;i++)
    > {
    > ...
    > int someVar=0;
    > someVar++;
    > ...
    > }
    >
    >
    >
    >
    > Or it really doesnt make any difference at all?


    For the POD like your 'int someVar' it most likely does not
    matter.

    Victor
    Victor Bazarov, Jul 1, 2003
    #2
    1. Advertising

  3. Vasileios Zografos

    Jack Klein Guest

    On Tue, 01 Jul 2003 22:25:42 +0100, Vasileios Zografos
    <> wrote in comp.lang.c++:

    > Ok, easy question.
    > Not caring about the variable scope what is better (i.e. possibly in
    > memory allocation etc)


    Have you read what you posted carefully? Does it really represent the
    question you wanted to ask?

    > int someVar=0;
    >
    > for (int i=0;i<1000;i++)
    > {
    > ...
    > someVar++;
    > ...
    > }


    In the example above, someVar will take on the values of 1 through
    1000 on successive iterations of the loop.

    > OR
    >
    > for (int i=0;i<1000;i++)
    > {
    > ...
    > int someVar=0;
    > someVar++;
    > ...
    > }


    In the example above, someVar will always be 0 at the point where it
    is defined and initialized, and always 1 after the post increment
    expression. It will never take on the values 2 through 1000, no
    matter how many times the loop executed.

    > Or it really doesnt make any difference at all?


    If this were real code, it certainly would make a difference.

    In principle, a variable defined inside a block is created when
    execution reaches the line that defines it, and is destroyed when
    control reaches the end of the block or leaves the block.

    If a variable defined inside a loop is a type with a non-trivial
    constructor and destructor, each of these will be called for each
    iteration of the loop. If the object is a primitive built-in type,
    and perhaps even a POD structure or class type, the compiler may
    generate code to allocate the memory once and reuse it each time, or
    it may decide to allocate and deallocate the memory by some method in
    each iteration of the loop. There is no requirement or guarantee
    either way.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Jul 1, 2003
    #3
  4. > Have you read what you posted carefully? Does it really represent the
    > question you wanted to ask?


    Yes
    >
    >
    >>int someVar=0;
    >>
    >>for (int i=0;i<1000;i++)
    >>{
    >> ...
    >> someVar++;
    >> ...
    >>}

    >
    >
    > In the example above, someVar will take on the values of 1 through
    > 1000 on successive iterations of the loop.
    >
    >
    >>OR
    >>
    >>for (int i=0;i<1000;i++)
    >>{
    >> ...
    >> int someVar=0;
    >> someVar++;
    >> ...
    >>}

    >
    >
    > In the example above, someVar will always be 0 at the point where it
    > is defined and initialized, and always 1 after the post increment
    > expression. It will never take on the values 2 through 1000, no
    > matter how many times the loop executed.


    yeah....I am not interested in the value of someVar. More on if it has
    some overhead on the construction/desctruction, memory and so on.

    > If this were real code, it certainly would make a difference.
    >
    > In principle, a variable defined inside a block is created when
    > execution reaches the line that defines it, and is destroyed when
    > control reaches the end of the block or leaves the block.
    >
    > If a variable defined inside a loop is a type with a non-trivial
    > constructor and destructor, each of these will be called for each
    > iteration of the loop. If the object is a primitive built-in type,
    > and perhaps even a POD structure or class type, the compiler may
    > generate code to allocate the memory once and reuse it each time, or
    > it may decide to allocate and deallocate the memory by some method in
    > each iteration of the loop. There is no requirement or guarantee
    > either way.
    >
    Vasileios Zografos, Jul 1, 2003
    #4
  5. > For the POD like your 'int someVar' it most likely does not
    > matter.
    >
    > Victor
    >
    >

    so, if instead int someVar it was something like

    vector<int> someVar

    ???
    Vasileios Zografos, Jul 1, 2003
    #5

  6. > Non-trivial constructor will be invoked during execution of
    > every loop cycle.
    >
    > I am not sure of the reason for your question, but if you are
    > concerned with performance,


    it was about performace really (or anyting else that I am missing)

    don't be until your code works.
    > If the design of your algorithm calls for the existence of
    > the object beyond the loop, then declare it before, otherwise
    > declare it inside. Only if later, once your algorithm is
    > working fine, you discover that it's unacceptably sluggish
    > due to multitude of calls to the constructor, you could try
    > to get rid of them. But at the beginning try following this
    > simple rule: do not declare an object until you actually need
    > it.


    ok I ll follow that rule
    Thanks
    Vasileios
    Vasileios Zografos, Jul 1, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The alMIGHTY N
    Replies:
    1
    Views:
    610
    Joris Gillis
    Feb 9, 2007
  2. Bolin
    Replies:
    4
    Views:
    395
  3. news.microsoft.com
    Replies:
    2
    Views:
    416
    Alan Silver
    Jul 22, 2007
  4. Ramon F Herrera
    Replies:
    11
    Views:
    1,346
    Joe Kesselman
    Jun 7, 2012
  5. taj sha
    Replies:
    0
    Views:
    684
    taj sha
    Aug 14, 2012
Loading...

Share This Page