Local variables defined internally for loops in C ++

In the following C ++ code snippet:

for (int i=0; i<10; i++) { int y = someFunctionCall(); //Some statements } 

Is it a variable (y) allocated every time the loop repeats and then is de-allocated during the iteration or does it allocate one time for all iterations of the loop?

Is the specified code equivalent to the following ?:

  int y; for (int i=0;i<10;i++) { y = someFunctionCall(); //Some statements } 
+7
source share
7 answers

It does not stand out every time, however, it assigns a new value at each iteration. The loop is inside a method that has its own stack stack. The variable y is allocated on this stack stack.

+3
source

It will be pushed onto the stack once when the function is called. Of course, there is no difference between the two ways of doing it (but recall that with the last method, y will still be in the area after the loop). That a variable is created and destroyed between each iteration (so that it "loses" its value between iterations) is the behavior that the compiler creates; the actual memory location remains the same all the time.

+5
source

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

However, for an int variable, this is not important. It is better to have a small area for the variable. And the compiler is probably smart enough to use the same space every time.

0
source

I think that the variable y will be allocated on the stack every time you get into the loop, because the variable will be flipped when it leaves this area.

0
source

It cannot be equivalent to the one you presented, because y is beyond the scope of the for loop.

But all this really depends on the compiler, you will need to measure the performance between them, if that is what you need.

0
source

y in your code is a local variable. Despite the fact that many people believe that they are not allocated. They may or may not have a place reserved for them on the stack, but this is only guaranteed if, after optimization, their address is fulfilled.

In all other cases, they may not have enough space on the stack. Or there may be some space on the stack that they use, but the same space is used for several variables, or even in some cases to pass arguments to the functions that you call.

When space is reserved on the stack (which may or may not happen), this space is usually reserved immediately after the function is entered. However, this is an implementation detail. This is done as it is the fastest way to do it. There is no need for this to be done, and the implementation can very well dynamically change the size of the current stack frame (on most modern processors this is a silly thing, but such an implementation will still be correct).

0
source

With built-in types, this does not matter (regardless of scope). With objects, whether an object variable in / from a for (!) Loop depends. 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 << aa << " | " << ai << std::endl; } return 0; } 
0
source

All Articles