3 registered members (Ayumi, Akow, AndrewAMD),
1,505
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Declaring vars in loops
[Re: jumpman]
#468238
09/26/17 19:26
09/26/17 19:26
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
That's the question I asked myself today Interested to see the answer! Edit: same for VECTOR and ANGLE and another stuff, if we declare them in the loop, how does engine handle that? Best regards!
Last edited by 3run; 09/26/17 20:59.
|
|
|
Re: Declaring vars in loops
[Re: 3run]
#468239
09/26/17 21:05
09/26/17 21:05
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
I would say that there is no difference. Each function has a memory area associated. It is allocated into the stack when the function starts and deallocated when it ends. The compiler is smart enough to use the same stack memory address each loop. In the end, each loop is part of the same function.
int _i = 0;
for ( ; _i<10; _i+=1 )
{
var _a = 1;
long *_aP = &_a;
printf ( "%i", _aP );
}
In most modern computer systems, each thread has a reserved region of memory referred to as its stack. When a function executes, it may add some of its state data to the top of the stack; when the function exits it is responsible for removing that data from the stack. At a minimum, a thread's stack is used to store the location of function calls in order to allow return statements to return to the correct location, but programmers may further choose to explicitly use the stack. If a region of memory lies on the thread's stack, that memory is said to have been allocated on the stack.
Because the data is added and removed in a last-in-first-out manner, stack-based memory allocation is very simple and typically faster than heap-based memory allocation (also known as dynamic memory allocation). Another feature is that memory on the stack is automatically, and very efficiently, reclaimed when the function exits, which can be convenient for the programmer if the data is no longer required. If however, the data needs to be kept in some form, then it must be copied from the stack before the function exits. Therefore, stack based allocation is suitable for temporary data or data which is no longer required after the creating function exits.
|
|
|
Re: Declaring vars in loops
[Re: txesmi]
#468247
09/27/17 07:39
09/27/17 07:39
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
Caution! This is the correct version:
action super_thing()
{
var Boop; //declared before the loop
while(1)
{
...
Boop+=1*time_step;
}
}
This is wrong:
action super_thing()
{
while(1)
{
var Boop; //declared before the loop
Boop+=1*time_step;
}
}
Yes, it will compile. Yes, it may work in some special cases. BUT: Boop is uninitialized and may contain ANY value possible.Look at this example:
#include <acknex.h>
function main()
{
int a = 10;
{
int b;
b += 20;
printf("1: %d %d", a, b);
}
{
int b;
b += 20;
printf("1: %d %d", a, b);
}
}
I got the following results: So you can't rely that local variables will be initialized. Also, if they are initialized with zero, you will get your variable reset each iteration of the loop.
|
|
|
Re: Declaring vars in loops
[Re: txesmi]
#468249
09/27/17 08:41
09/27/17 08:41
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
It's the scope. As soon as your (loop) body ends, the variables value is not guaranteed to be valid anymore. So in the next iteration of the loop, the value is again undefined and you have to initialize it again.
action super_thing()
{
while(1)
{ // Boop is "allocated" here
var Boop; // Boop is undefined here
Boop+=1*time_step; // add something to an undefined value will result in an undefined value
} // Boop is "invalidated" here
}
If you want to change a variable over multiple operations (like in the code example), you need to do this:
action super_thing()
{ // Boop is "allocated" here
var Boop = 0; // Boop is defined here
while(1)
{
Boop+=1*time_step; // add something iteratively to the initially defined value
}
} // Boop is "invalidated" here
|
|
|
Re: Declaring vars in loops
[Re: MasterQ32]
#468250
09/27/17 09:01
09/27/17 09:01
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
I know you can not spect rational results from an uninitialized variable and you can not spect a variable inherit a value during its declaration, so you need to initialize it in any case. I tryed to ask if there was any problem with declaring and initializing a variable for its usage inside its scope. f.e:
int _x = 0;
for ( ; _x<SIZE_X; _x+=1 )
{
int _y = 0;
for ( ; _y<SIZE_Y; _y+=1 )
{
...
Thanks!
|
|
|
|