Raised This Month: $51 Target: $400
 12% 

[TUT] The Use of Static Variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-26-2006 , 09:10   [TUT] The Use of Static Variables
Reply With Quote #1

What is a static variable? It is basically a global variable that can only be accessed by the function it is declared in.

This means that:

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("The Internet","Has","Spoken")         fnDoFunc()     iNum = 2 } fnDoFunc() {     static iNum     iNum = 1 }

Is NOT valid. Why? Because the static variable was declared in fnDoFunc, not plugin_init.

The example above does not exemplify the true power of static variables. If you're just going to zero it before you use it, use the operator "new". Declaring a new variable is not an expensive operation, simply the zeroing of it is.

But what can a static variable do?

Here's an example of the usage of a static variable vs. a new variable:

Static
Code:
#include <amxmodx> #include <amxmisc> public plugin_init()     register_plugin("The Internet","Has","Spoken") public client_connect(id) {     static szName[33]         get_user_name(id,szName,32)     client_print(0,print_chat,"the matrix has %s",szName) }

New

Code:
#include <amxmodx> #include <amxmisc> public plugin_init()     register_plugin("The Internet","Has","Spoken") public client_connect(id) {     new szName[33]     get_user_name(id,szName,32)     client_print(0,print_chat,"the matrix has %s",szName) }

But why is the static version faster? Because the memory isn't created every single time it's needed, rather it's left there, kind of like a global variable never loses its value ever after being used in a function.

So for example, take in this hypothetical sequence of events in a server:

Static
Code:
  static variable initialized
  
  Hawk552 joins server
  client_connect called
  static variable already exists
  get name
  print name
  
  *5 minutes later*
  
  zomg joins server
  client_connect called
  static variable already exists with contents "Hawk552\0..."
  get name -> now looks like "zomg\052\0...", however it ends at the first \0 so the trailing 52 doesn't matter
  print name
New
Code:
  Hawk552 joins server
   client_connect called
  new variable initialized
   get name
   print name
  
  *5 minutes later*
  
  zomg joins server
    client_connect called
   new variable initialized
    get name
    print name
And that is basically why static variables are useful.
__________________

Last edited by Hawk552; 06-30-2006 at 10:32. Reason: merged in... oh hello pm
Hawk552 is offline
Send a message via AIM to Hawk552
3xr
Junior Member
Join Date: Mar 2005
Old 06-26-2006 , 16:51   Re: The Use of Static Variables
Reply With Quote #2

Thanks for the info. BTW: Happy Birthday.
3xr is offline
Cheap_Suit
Veteran Member
Join Date: May 2004
Old 06-27-2006 , 03:02   Re: The Use of Static Variables
Reply With Quote #3

hmmm interesting...

Can you tell me when to use it and when not to use it
__________________
HDD fried, failed to backup files. Sorry folks, just don't have free time anymore. This is goodbye.
Cheap_Suit is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-27-2006 , 09:37   Re: The Use of Static Variables
Reply With Quote #4

Quote:
Originally Posted by Cheap_Suit
hmmm interesting...

Can you tell me when to use it and when not to use it
Well, in something like a loop, it's pretty useless, like this:

Code:
for(static iCount = 0;iCount < x;iCount++)

You'd might as well use new, because the = 0 section of it will make it the same expensiveness as using new. This goes for most other things like = get_user_frags(id) etc.

When using a huge ass array like a motd or menu it's generally best to use static because arrays are a huge hit on CPU, and making it static nullifies that. You must be careful about re-entrancy though, as in something being set from the last time it was used and using it for this instance anyway. If your script is enormous you might start running out of memory in which case you can use #pragma dynamic, with some value like 131072 (which is in bytes)

But as shown in my example one of the best ways to use this is something like:

Code:
static szName[33] get_user_name(id,szName,32)
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Orangutanz
Veteran Member
Join Date: Apr 2006
Old 06-27-2006 , 21:21   Re: The Use of Static Variables
Reply With Quote #5

Quote:
Originally Posted by Cheap_Suit
Can you tell me when to use it and when not to use it
I would imagine a better example would of been if using Forwards such as: PlayerPreThink, StartFrame, Touch etc
All of which are very demanding forwards, so a static would be handy to reduce overhead in the long term.

One annoying thing with static is that you cannot assign as you can with new, example:
Code:
new team = get_user_team(id)
static team
team = get_user_team(id)
Also I'm uncertain about this as well:
Code:
static team, player
Would player be a static or a new? I assume static but we all know how buggy the Pawn compiler is!

Last edited by Orangutanz; 06-27-2006 at 21:24.
Orangutanz is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-27-2006 , 22:02   Re: The Use of Static Variables
Reply With Quote #6

Quote:
Originally Posted by Orangutanz
I would imagine a better example would of been if using Forwards such as: PlayerPreThink, StartFrame, Touch etc
All of which are very demanding forwards, so a static would be handy to reduce overhead in the long term.

One annoying thing with static is that you cannot assign as you can with new, example:
Code:
new team = get_user_team(id)
static team
team = get_user_team(id)
Also I'm uncertain about this as well:
Code:
static team, player
Would player be a static or a new? I assume static but we all know how buggy the Pawn compiler is!
I'm pretty sure that in your last example they would both be static.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
BAILOPAN
Join Date: Jan 2004
Old 06-27-2006 , 22:05   Re: The Use of Static Variables
Reply With Quote #7

Using static isn't really necessary if you initialize it like that. It's more appropriate for arrays.
__________________
egg
BAILOPAN is offline
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 07-04-2006 , 09:39   Re: The Use of Static Variables
Reply With Quote #8

(clears throat)
Quote:
Sorry; I didn't mean to be impolite,
He is apologizing.
Quote:
just wanted to clear that up in case anyone thought that a static variable
He is announcing what he is clearing up.
Quote:
suddenly pops up when the line of code where it was declared is executed and then remains there until the universe implodes.
Actually thats worded a bit funky... He is saying when a line of code, is declared, and is executed, in the plugin, that the person whos viewing the code, won't think that the static variable, will stay there untill the end of time... Does that clear it up a bit?
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))
TheNewt is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 07-04-2006 , 10:06   Re: The Use of Static Variables
Reply With Quote #9

Oh perhaps I should have written it more clearly.

The static variable "exists" as long as the plugin is loaded. It is loaded into memory (as a part of the plugin's DAT section) when the plugin is loaded (that is, on map start), and it is freed when the plugin is unloaded (on map end / server shutdown). My point was, the static variable is not "created" when the line it is declared in is first reached. It is in memory as long as the plugin is loaded.
__________________
hello, i am pm
PM is offline
Old 08-26-2006, 09:35
Hawk552
This message has been deleted by Hawk552.
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:39.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode