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

[TUT] The Use of Static Variables


Post New Thread Reply   
 
Thread Tools Display Modes
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 06-30-2006 , 08:35   Re: The Use of Static Variables
Reply With Quote #11

Quote:
Originally Posted by v3x
oh, hello pm,

maybe you should make a tutorial on the use of static variables instead
Sorry; I didn't mean to be impolite, just wanted to clear that up in case anyone thought that a static variable suddenly pops up when the line of code where it was declared is executed and then remains there until the universe implodes.
__________________
hello, i am pm
PM is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 06-30-2006 , 10:23   Re: The Use of Static Variables
Reply With Quote #12

Quote:
Originally Posted by PM
Sorry; I didn't mean to be impolite, just wanted to clear that up in case anyone thought that a static variable suddenly pops up when the line of code where it was declared is executed and then remains there until the universe implodes.
I'll edit this in, and thanks, I was under the impression that after they're initialized they stay in the the .DATA section.
__________________

Last edited by Hawk552; 07-01-2006 at 11:19.
Hawk552 is offline
Send a message via AIM to Hawk552
Cheap_Suit
Veteran Member
Join Date: May 2004
Old 07-03-2006 , 20:36   Re: The Use of Static Variables
Reply With Quote #13

Quote:
Originally Posted by v3x
oh, hello pm,

maybe you should make a tutorial on the use of static variables instead
No offence, but if PM writes them. Noobs like myself will have a hard time understanding them. Like what he just wrote!

Anyways, Thanks for you info!
__________________
HDD fried, failed to backup files. Sorry folks, just don't have free time anymore. This is goodbye.
Cheap_Suit 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 #14

(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 #15

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.
Old 08-26-2006, 10:44
Hawk552
This message has been deleted by Hawk552.
Identity
Member
Join Date: Mar 2007
Old 03-09-2007 , 17:16   Re: [TUT] The Use of Static Variables
Reply With Quote #16

Code:
static variable initialized
  
  Hawk552 joins server
  client_connect called (by Hawk552)
  zomg joins server
  client_connect called (by zomg) 
  static variable already exists
  get name (by Hawk552)
  get name (by zomg)-> now looks like "zomg\052\0...", however it ends at the first \0 so the trailing 52 doesn't matter
  print name (by Hawk552)
  print name (by zomg)
In this case it'll print zomg twice?
Identity is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 03-09-2007 , 17:29   Re: [TUT] The Use of Static Variables
Reply With Quote #17

Quote:
Originally Posted by Identity View Post
Code:
static variable initialized
  
  Hawk552 joins server
  client_connect called (by Hawk552)
  zomg joins server
  client_connect called (by zomg) 
  static variable already exists
  get name (by Hawk552)
  get name (by zomg)-> now looks like "zomg\052\0...", however it ends at the first \0 so the trailing 52 doesn't matter
  print name (by Hawk552)
  print name (by zomg)
In this case it'll print zomg twice?
Uhh... what?
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Identity
Member
Join Date: Mar 2007
Old 03-09-2007 , 20:13   Re: [TUT] The Use of Static Variables
Reply With Quote #18

Quote:
Uhh... what?
I mean, if two(or many) players connect at same time, static variable szName will be overwritten and client_print display incorrect results?
Identity is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 03-09-2007 , 20:20   Re: [TUT] The Use of Static Variables
Reply With Quote #19

Quote:
Originally Posted by Identity View Post
I mean, if two(or many) players connect at same time, static variable szName will be overwritten and client_print display incorrect results?
No, it won't. HL isn't multi-threaded, and even if it was, the entire function is executed at once. That can never, ever possibly happen.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Old 03-09-2007, 22:04
Identity
This message has been deleted by Hawk552.
Old 03-09-2007, 22:19
Hawk552
This message has been deleted by Hawk552.
Dr. Jan Itor
Veteran Member
Join Date: Mar 2008
Location: there.
Old 07-20-2008 , 14:52   Re: [TUT] The Use of Static Variables
Reply With Quote #20

what difference do the numbers have? like whats the diff on
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)
}
and

Code:
#include <amxmodx>
#include <amxmisc>

public plugin_init()
    register_plugin("The Internet","Has","Spoken")

public client_connect(id)
{
    static szName[128]
    
    get_user_name(id,szName,127)
    client_print(0,print_chat,"the matrix has %s",szName)
}
__________________

Last edited by Dr. Jan Itor; 07-20-2008 at 14:55.
Dr. Jan Itor is offline
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 07:10.


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