Declaring vars in loops

Posted By: jumpman

Declaring vars in loops - 09/26/17 19:10

Hi, is there a difference between declaring a var at the beginning of an action, compared to declaring a var within the actions loop?

action super_thing()
{

var Boop; //declared before the loop

while(1)
{
...
Boop+=1*time_step;
}

}

____________Compared to this:

action super_thing()
{


while(1)
{
var Boop; //declared before the loop

Boop+=1*time_step;

}

}
Posted By: jumpman

Re: Declaring vars in loops - 09/26/17 19:16

aside from the second example having Boop stay the same, does the engine continuously allocate memory every time the var is declared within the loop?
Posted By: 3run

Re: Declaring vars in loops - 09/26/17 19:26

That's the question I asked myself today grin 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!
Posted By: txesmi

Re: Declaring vars in loops - 09/26/17 21:05

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.

Code:
int _i = 0;
for ( ; _i<10; _i+=1 )
{
	var _a = 1;
	long *_aP = &_a;
	printf ( "%i", _aP );
}



Originally Posted By: wikipedia

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.
Posted By: 3run

Re: Declaring vars in loops - 09/26/17 21:21

Thank you very much, txesmi!
Posted By: txesmi

Re: Declaring vars in loops - 09/27/17 07:30

glad to help wink
Posted By: MasterQ32

Re: Declaring vars in loops - 09/27/17 07:39

Caution!

This is the correct version:
Code:
action super_thing()
{
  var Boop; //declared before the loop
  while(1)
  {
    ...
    Boop+=1*time_step;
  }
}




This is wrong:
Code:
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:
Code:
#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:
Originally Posted By: Run 1

1: 10 3406956
2: 10 3810


Originally Posted By: Run 2

1: 10 20
2: 10 99345


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.
Posted By: txesmi

Re: Declaring vars in loops - 09/27/17 08:17

What's wrong in declaring and initializing a variable inside a loop?
I like to declare the variables near to its usage, it helps on keeping things clear.
Posted By: MasterQ32

Re: Declaring vars in loops - 09/27/17 08:41

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.

Code:
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:

Code:
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

Posted By: txesmi

Re: Declaring vars in loops - 09/27/17 09:01

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:
Code:
int _x = 0;
for ( ; _x<SIZE_X; _x+=1 )
{
   int _y = 0;
   for ( ; _y<SIZE_Y; _y+=1 )
   {
      ...



Thanks!
Posted By: MasterQ32

Re: Declaring vars in loops - 09/27/17 10:00

As long as the variable is only used within the loops body (and not expecting to keep its value over loop iterations), it's perfectly fine and the good way.
Posted By: jumpman

Re: Declaring vars in loops - 09/27/17 14:36

thank you for the indepth look into this friends.

When looking at the "variables" page in the manual, it describes the var types, short/char/var/int/float, and each of these is also described as size in bytes, which I assume is how much memory it requires to use. So if you declare boop before the while(1) loop, the engine allocated 8bytes of memory for this variable, and never has to allocate memory for this again.

But if you declare boop within a loop, is the engine continually adding 8bytes every frame?....which means if you leave it on long enough the engine would run out of memory lol, which I think doesnt happen.

Code:
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
}



this is where it made sense for me laugh Thank you
Posted By: MasterQ32

Re: Declaring vars in loops - 09/27/17 16:12

Quote:
But if you declare boop within a loop, is the engine continually adding 8bytes every frame?....which means if you leave it on long enough the engine would run out of memory lol, which I think doesnt happen.

No, this is not right. Allocation in this context is allocating stack memory which is released at the end of a block (which is a closing '}')
© 2024 lite-C Forums