Quote:
Originally Posted by Exolent[jNr]
New vs. Static
You should only be using these for variables.
New- Creates a new variable in memory every time it is declared
- At the end of this variable's scope, it is deleted from memory and no longer accessible
Static- At the end of this variable's scope, it is deleted from memory and no longer accessible
|
Partially true.
AMX file contain .DATA section to store values for constants and global variables. This section filled at compile time (Thats why we cant initialize static and constant variables using runtime functions). Some example:
PHP Code:
const var1 = 0;
new var2;
static var3;
func()
{
const var4 = 0;
static var5;
}
//here var4 still in memory, and not deleted
At run time we can access .DATA section, we can change it (even constants using some methods), but can`t add cells (new variables) to it.
PHP Code:
func()
{
new var1 = 5;
new var2[2048];
}
Here we create variables (at run time) in STACK of AMXX Virtual Machine, each time var is created it fills with 0 or constant value. Thats why var2[2048] is bad idea in frequently accessed functions. After loosing variable scope compiler clears STACK (here it totally removes from "memory"). To return array from functions compiler using another type of memory - HEAP (it`s not directly for variables, but why not to know about it
).
So, with "static" keyword we can create variables only in .DATA section of AMX file. With "new" keyword in .DATA section and STACK. For me better to use "new" keyword to define only local variables, to avoid double meaning of that keyword:
PHP Code:
#include "MyInc"
stock var1;
func()
{
new var2;
}