Raised This Month: $ Target: $400
 0% 

Static variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Prajch
Senior Member
Join Date: Dec 2007
Location: anger++
Old 08-28-2008 , 10:18   Static variables
Reply With Quote #1

If I write something like
PHP Code:
public client_connect(id)
{
   static 
authid[35]
   
get_user_authid(idauthid34)
 
   ...

Is it possible that the contents of authid are overwritten before the entire routine has been executed?
Prajch is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-28-2008 , 12:00   Re: Static variables
Reply With Quote #2

Static variables are just like global variables,but with a local scope.Once you assign something to them, it will stay there until you do it again.

Last edited by danielkza; 08-28-2008 at 12:01. Reason: Worst typo ever
danielkza is offline
Prajch
Senior Member
Join Date: Dec 2007
Location: anger++
Old 08-28-2008 , 12:12   Re: Static variables
Reply With Quote #3

I guess a better question would be whether it's possible for client_connect (in this case) to be called again before the first call is finished, thus overwriting authid.
Prajch is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-28-2008 , 12:43   Re: Static variables
Reply With Quote #4

Quote:
Originally Posted by Prajch View Post
I guess a better question would be whether it's possible for client_connect (in this case) to be called again before the first call is finished, thus overwriting authid.
No, HL is single-thread,nothing will run until your function finishes.
danielkza is offline
Prajch
Senior Member
Join Date: Dec 2007
Location: anger++
Old 08-28-2008 , 13:09   Re: Static variables
Reply With Quote #5

Ok thank you.
Prajch is offline
Silencer123
Veteran Member
Join Date: Jul 2006
Old 08-28-2008 , 16:21   Re: Static variables
Reply With Quote #6

You by the way cannot return authid in client_connect.(id)
The earliest moment you can do so is client_authorized(id).
__________________
EAT YOUR VEGGIES
Silencer123 is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 08-28-2008 , 16:30   Re: Static variables
Reply With Quote #7

It's also not necessarily worth it to use a static variable in this particular case as the forward isn't get called excessively nor is your variable of an excessive length.

If you ran a timing test, you wouldn't notice much of a difference, if any, unless you ran it an absurd number of times.
__________________
Brad is offline
Prajch
Senior Member
Join Date: Dec 2007
Location: anger++
Old 08-28-2008 , 16:59   Re: Static variables
Reply With Quote #8

It was just an example; I was more interested in other cases, but thanks for the extra tips.
Prajch is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-28-2008 , 17:11   Re: Static variables
Reply With Quote #9

The Use of Static Variables
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 03:07.


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