Raised This Month: $ Target: $400
 0% 

global new or static?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Emp0wer
Member
Join Date: Jul 2010
Location: Ukraine mother
Old 08-23-2010 , 15:00   global new or static?
Reply With Quote #1

What is better?

PHP Code:
new g_classname[32]

public 
fw_Think(entity)
{
    
pev(entity,pev_classname,g_classname,31)

PHP Code:
public fw_Think(entity)
{
    new 
classname[32]
    
pev(entity,pev_classname,classname,31)

PHP Code:
public fw_Think(entity)
{
    static 
classname[32]
    
pev(entity,pev_classname,classname,31)

Emp0wer is offline
Send a message via ICQ to Emp0wer Send a message via Skype™ to Emp0wer
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 08-23-2010 , 15:03   Re: global new or static?
Reply With Quote #2

well you gotta think abot what you are using it for. Global is used so that you can use it in more than one function. New is limited to only that function, as well as static.

http://wiki.amxmodx.org/Optimizing_P...atic_vs_Global
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-23-2010 , 15:07   Re: global new or static?
Reply With Quote #3

For arrays used in a forward called often, you better use static. 1 is the same than 3 except the scope of the var is expanded all the functions.
__________________
Arkshine is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 08-23-2010 , 16:42   Re: global new or static?
Reply With Quote #4

About that document ( http://wiki.amxmodx.org/Optimizing_P...atic_vs_Global )... I can't really understand this part:
Quote:
Note: Be careful of re-entrancy!
When a variable is static, it has the same re-entrancy problems of a global variable. That means, if your function might be called recursively, or twice in the same stack frame, you should not use static variables. This is most often the case for API provided to other plugins or helper functions. Triggered events are usually never called twice on the same execution chain.
__________________

Last edited by Hunter-Digital; 08-23-2010 at 16:44.
Hunter-Digital is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-23-2010 , 16:56   Re: global new or static?
Reply With Quote #5

If both functions A and B use function C and C is using static variable then you might not get what expected. If A executes first then B it might not be the same as B executing first then A. The outcome of A and B might be different.

Something along those lines. It's similar to race conditions I believe.
__________________
fysiks is offline
Emp0wer
Member
Join Date: Jul 2010
Location: Ukraine mother
Old 08-23-2010 , 17:07   Re: global new or static?
Reply With Quote #6

Ok I got it Thx for answers
Emp0wer is offline
Send a message via ICQ to Emp0wer Send a message via Skype™ to Emp0wer
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 22:03.


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