06.07.2011, 15:07
Hmm it is quite hard to explain.
The stack memory used only counts for each "stage" and its previous "stages" (I don't know how to call it).
In this example OnGameModeInit uses 128 cells, while OnGameModeExit uses 384 cells (256+128 cells).
However, OnPlayerConnect only uses 512 cells instead of 640. This is because string3 is in another "stage" that the memory will be released after the "stage" ends. However the memory of string and string2 is still there, because the "stage" which declare them haven't ended.
For the stack size you see when you finish compiling, it will only count for each function, and find the maximum stack memory used in these functions. So in this case it will be 512 cells.
However if you call another function in a function, the memory used in the function called will be counted as part of the stack memory usage of the original function. So if you do recursion the compiler will say the stack size is unknown (infinity).
You need to take care on the stack size. If it exceed the maximum size allowed, the data in memory can be corrupted.
Finally here's a wiki about it:http://en.wikipedia.org/wiki/Stack_(data_structure)
pawn Код:
new playerinfo[MAX_PLAYERS][20];//A global variable which will not use stack memory
public OnGameModeInit()
{
new string[128];//A local variable that it will use stack memory
return 1;
}
public OnGameModeExit()
{
new string[256],string2[128];//Local variables that it will use stack memory
return 1;
}
public OnPlayerConnect(playerid)
{
new string[256],string2[128];//Local variables that it will use stack memory
{
new string3[128];//..
}
{
new string4[128];//..
}
return 1;
}
In this example OnGameModeInit uses 128 cells, while OnGameModeExit uses 384 cells (256+128 cells).
However, OnPlayerConnect only uses 512 cells instead of 640. This is because string3 is in another "stage" that the memory will be released after the "stage" ends. However the memory of string and string2 is still there, because the "stage" which declare them haven't ended.
For the stack size you see when you finish compiling, it will only count for each function, and find the maximum stack memory used in these functions. So in this case it will be 512 cells.
However if you call another function in a function, the memory used in the function called will be counted as part of the stack memory usage of the original function. So if you do recursion the compiler will say the stack size is unknown (infinity).
You need to take care on the stack size. If it exceed the maximum size allowed, the data in memory can be corrupted.
Finally here's a wiki about it:http://en.wikipedia.org/wiki/Stack_(data_structure)