9

In the following piece of C++ code:

    for (int i=0; i<10; i++)
    {
        int y = someFunctionCall();

        //Some statements
    }

is the variable (y) allocated each time the loop iterate and then de-allocated when the iteration is done, or it's allocated one time for all loop iterations?

Is the mentioned code equivalent to the following?:

    int y;
    for (int i=0;i<10;i++)
    {
        y = someFunctionCall();

        //Some statements
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmed Adel
  • 331
  • 4
  • 10

7 Answers7

6

It will be allocated on the stack once, when the function is called. Performance-wise, there is no difference between the two ways of doing it (but recall that with the last way, y will still be in scope after the loop). That the variable appears to be created and destroyed between each iteration (so that it "loses" its value between iterations) is a behavior that is created by the compiler; the actual memory location is the same all of the time.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
3

It's not allocated each time, however it's assigned a new value in each iteration. The loop is within a method, which has its own stack frame. The variable y is allocated in that stack frame.

Man of One Way
  • 3,904
  • 1
  • 26
  • 41
1

A new variable is created for each turn in the loop.

However, for an int type variable that is not important. It is better to have a small scope for the variable. And the compiler is probably smart enough to reuse the same space each time.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

I think the variable y will be allocate on the stack every time when you get into the loop cause the variable will be dellocated when it leaves this scope.

rpbear
  • 640
  • 7
  • 15
0

It couldn't be equivalent to the one you presented, because y would be out of the scope of the for loop.

But it all really depends on the compiler, you'd have to measure the performance between the two if that's what you're after for.

nullpotent
  • 9,162
  • 1
  • 31
  • 42
0

The y in your code is a local variable. Despite what many people think these are not allocated. They may or may not have some space reserved for them on the stack, but this is only guaranteed to happen if, after optimizations, their address is taken.

In all other cases they may not have space reserved on the stack at all. Or there may be some space on the stack that they use, but the same space is reused for multiple variables or even, in some cases, for passing arguments to functions that you are calling.

When space is reserved on the stack (which, may or may not happen) that space is generally reserved all at once upon function entry. However this is an implementation detail. It's done like that as it's the fastest way to do it. There is no requirement for it to be done the way and an implementation may very well dynamically change the size of the current stack frame (on most modern processors it's a stupid thing to do, but such an implementation would still be correct).

Analog File
  • 5,280
  • 20
  • 23
0

With in-build types it does not really matter (regardless of the scope of visibility). With objects, whether the object variable stays in/out of the for loop does(!) matter. Consider the following:

#include <iostream>

struct A
{

  static int i;
  void * a;

  A() : a(this) { ++i; }

};

int A::i = 0;

int main()
{

    for (int i = 0; i != 10; ++i)
    {

      A a;

      std::cout << a.a << " | " << a.i << std::endl;

    }

    return 0;
}
PavDub
  • 71
  • 4