Raised This Month: $ Target: $400
 0% 

Solved When should i use "static" over "new" variable?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Syturi0
Veteran Member
Join Date: Aug 2014
Location: Your mom house -Portugal
Old 03-03-2018 , 08:59   When should i use "static" over "new" variable?
Reply With Quote #1

In what situations should i use "static" over "new"? I dont get it.
Only when i dont need to empty/zero it first before using?

Last edited by Syturi0; 03-04-2018 at 14:02.
Syturi0 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-03-2018 , 09:06   Re: When should i use "static" over "new" variable?
Reply With Quote #2

Just search, there are literally 100 tutorials and questions about this.
__________________
HamletEagle is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 03-04-2018 , 07:25   Re: When should i use "static" over "new" variable?
Reply With Quote #3

new gets deleted in the end of the function, static is saved in the end of the function.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
Syturi0
Veteran Member
Join Date: Aug 2014
Location: Your mom house -Portugal
Old 03-04-2018 , 07:35   Re: When should i use "static" over "new" variable?
Reply With Quote #4

Quote:
Originally Posted by eyal282 View Post
new gets deleted in the end of the function, static is saved in the end of the function.
I knew that already.
But i still dont get it why would i use "new" over "static" and vice versa.
If "static" is faster and does the same job as "new", why dont i use "static" all the times?
Examples would be nice.

Last edited by Syturi0; 03-04-2018 at 07:37.
Syturi0 is offline
Syturi0
Veteran Member
Join Date: Aug 2014
Location: Your mom house -Portugal
Old 03-04-2018 , 07:35   Re: When should i use "static" over "new" variable?
Reply With Quote #5

Quote:
Originally Posted by HamletEagle View Post
Just search, there are literally 100 tutorials and questions about this.
And not a single one explains it properly.
Syturi0 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 03-04-2018 , 07:46   Re: When should i use "static" over "new" variable?
Reply With Quote #6

Use static for large arrays (at least like 500 or 1000 elements) unless the function declaring them is called rarely, or if you want a variable to preserve its value between function calls.
If at any time you spend at least a few seconds thinking whether you should go with static or new, just go with new, it won't matter. People are making a fuss about this as if the difference is noticeable, and also I've seen people slamming static over almost everything in their code which is just dumb.
__________________

Last edited by klippy; 03-04-2018 at 07:46.
klippy is offline
Syturi0
Veteran Member
Join Date: Aug 2014
Location: Your mom house -Portugal
Old 03-04-2018 , 07:49   Re: When should i use "static" over "new" variable?
Reply With Quote #7

Quote:
Originally Posted by KliPPy View Post
Use static for large arrays (at least like 500 or 1000 elements) unless the function declaring them is called rarely, or if you want a variable to preserve its value between function calls.
If at any time you spend at least a few seconds thinking whether you should go with static or new, just go with new, it won't matter. People are making a fuss about this as if the difference is noticeable, and also I've seen people slamming static over almost everything in their code which is just dumb.
Finally a GOOD answer.
Thank you.
Syturi0 is offline
E1_531G
Senior Member
Join Date: Dec 2017
Old 03-04-2018 , 08:23   Re: When should i use "static" over "new" variable?
Reply With Quote #8

For example, we have client_prethink() which is executed every client frame.
If inside the function you use new var, you will have:
1. create var
2. use var
3. delete var (on func end)
If you have static var instead:
1. create var (only first call)
2. use var
(never delete on func end)
__________________
My English is A0
E1_531G is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 03-04-2018 , 09:15   Re: When should i use "static" over "new" variable?
Reply With Quote #9

Quote:
Originally Posted by E1_531G View Post
For example, we have client_prethink() which is executed every client frame.
If inside the function you use new var, you will have:
1. create var
2. use var
3. delete var (on func end)
If you have static var instead:
1. create var (only first call)
2. use var
(never delete on func end)
It doesn't matter.
__________________
klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-04-2018 , 13:09   Re: When should i use "static" over "new" variable?
Reply With Quote #10

Quote:
Originally Posted by KliPPy View Post
It doesn't matter.
Step 1: new array[1000] in prethink
Step 2: enjoy.
__________________
HamletEagle 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 17:24.


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