Gamestudio Links
Zorro Links
Newest Posts
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
folder management functions
by 7th_zorro. 04/15/24 10:10
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
SGT_FW
by Aku_Aku. 04/10/24 16:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, Quad), 373 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Declaring vars in loops #468236
09/26/17 19:10
09/26/17 19:10
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
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;

}

}

Re: Declaring vars in loops [Re: jumpman] #468237
09/26/17 19:16
09/26/17 19:16
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
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?

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 Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
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!

Last edited by 3run; 09/26/17 20:59.

Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
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 Offline
Serious User
txesmi  Offline
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.

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.

Re: Declaring vars in loops [Re: txesmi] #468240
09/26/17 21:21
09/26/17 21:21
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Thank you very much, txesmi!


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Declaring vars in loops [Re: 3run] #468246
09/27/17 07:30
09/27/17 07:30
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
glad to help wink

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 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
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.


Visit my site: www.masterq32.de
Re: Declaring vars in loops [Re: MasterQ32] #468248
09/27/17 08:17
09/27/17 08:17
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
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.

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 Offline
Expert
MasterQ32  Offline
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.

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



Visit my site: www.masterq32.de
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 Offline
Serious User
txesmi  Offline
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:
Code:
int _x = 0;
for ( ; _x<SIZE_X; _x+=1 )
{
   int _y = 0;
   for ( ; _y<SIZE_Y; _y+=1 )
   {
      ...



Thanks!

Re: Declaring vars in loops [Re: txesmi] #468256
09/27/17 10:00
09/27/17 10:00
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
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.


Visit my site: www.masterq32.de
Re: Declaring vars in loops [Re: MasterQ32] #468265
09/27/17 14:36
09/27/17 14:36
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
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

Re: Declaring vars in loops [Re: jumpman] #468270
09/27/17 16:12
09/27/17 16:12
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
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 '}')


Visit my site: www.masterq32.de
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1