Raised This Month: $32 Target: $400
 8% 

Shouldn't the stocks inside .inc files always use "static"?


Post New Thread Reply   
 
Thread Tools Display Modes
PartialCloning
Senior Member
Join Date: Dec 2015
Old 07-23-2018 , 18:20   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #11

Why is everyone implying you shouldn't use static!? You should use static for large arrays in forwards that are fired repeatedly in quick intervals, or you will get a stack error unless you use #pragma dynamic to increase the size of the memory block.

static should not be used in the default amx plugins, that's a poor solution to poor usage.
PartialCloning is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 07-23-2018 , 18:32   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #12

Stack overflows from large local arrays has nothing to do with the frequency of calls.
__________________
asherkin is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 07-23-2018 , 18:51   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #13

PHP Code:
public server_frame()
{
    
server_print("server_frame");
    new 
test[12345];
    new 
test2[12345];
    new 
test3[12345];

Quote:
server_frame
[AMXX] Run time error 3: stack error
server_frame
[AMXX] Run time error 3: stack error
server_frame
[AMXX] Run time error 3: stack error
...
PHP Code:
public server_frame()
{
    
server_print("server_frame");
    static 
test[12345];
    static 
test2[12345];
    static 
test3[12345];

Quote:
server_frame
server_frame
server_frame
server_frame
...
PartialCloning is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-23-2018 , 20:09   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #14

It still has nothing to do with frequency, only the size.
__________________
klippy is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-27-2018 , 20:54   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #15

I understood static declaration the same as OciXCrom..allocate the memory once and it is re-used each time instead of re-allocated. Though asherkins point is also true, static allows local variables to retain values between function calls, similar to a global, but with local scope..while I don't think this is the reason for its existence.

wiki says it too, so it has to be true
Code:
In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program.
__________________

Last edited by Bugsy; 07-27-2018 at 20:54.
Bugsy is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-28-2018 , 07:52   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #16

Quote:
Originally Posted by Bugsy View Post
I understood static declaration the same as OciXCrom..allocate the memory once and it is re-used each time instead of re-allocated. Though asherkins point is also true, static allows local variables to retain values between function calls, similar to a global, but with local scope..while I don't think this is the reason for its existence.

wiki says it too, so it has to be true
Code:
In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program.
Finally, I'm not the only one. It says that everywhere though, even when plugin approvers review my plugin submissions they say I should use static for functions that are called often.

Quote:
Originally Posted by HamletEagle
We should see what "often" really mean. Making them static only worth if the function is called per frame(i.e PreThink, AddToFullPack, CmdStart, etc) or if you are working with very big arrays(so you don't overflow anything).
Even if you have a task that repeats at 1.0 seconds and only small arrays static is not needed.
You could also use static on a global variable if you work with multiple files(i.e custom includes) and you want a variable to be accesible only in the scope of the file in which it was create.

Using static in other cases is pretty much over-optimization and for the sake of good practices let's use new.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-05-2018 , 03:04   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #17

Quote:
Originally Posted by Bugsy View Post
Though asherkins point is also true, static allows local variables to retain values between function calls, similar to a global, but with local scope..while I don't think this is the reason for its existence.
Actually that's the exact reason for its existence.
Also declaring a single cell variable as static then immediately unconditionally setting its value is not any better than using new var =... , performance wise.
__________________

Last edited by klippy; 08-05-2018 at 03:09.
klippy is offline
olotfi
New Member
Join Date: Aug 2018
Old 08-07-2018 , 07:13   Re: Shouldn't the stocks inside .inc files always use "static"?
Reply With Quote #18

I come from a scripting background and the preprocessor in C has always seemed ugly to me. None the less I have embraced it as I learn to write small C programs. I am only really using the preprocessor for including the standard libraries and header files I have written for my own functions. CCleaner Happy Wheels VLC

Last edited by olotfi; 08-08-2018 at 18:54.
olotfi is offline
Old 09-06-2018, 05:29
san C
This message has been deleted by asherkin. Reason: Spam.
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 05:04.


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