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

Use NEW or not?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Cooltad
Veteran Member
Join Date: Apr 2008
Old 01-18-2010 , 09:07   Use NEW or not?
Reply With Quote #1

I wish to know, which method is better?
Code:
new valid_weapon;

blah blah code..

valid_weapon=blahblah;
OR

Code:
blah blah code...

new valid_weapon=blahblah
__________________
Please, give me some rep if you found what I posted useful. :]

Last edited by Cooltad; 01-18-2010 at 09:11.
Cooltad is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 01-18-2010 , 09:24   Re: Use NEW or not?
Reply With Quote #2

It's a common practice to allocate memory ressources at the top of the function/namespace. I think the reason is primarily the code layout,
and I think in the past the compilers didn't optimize allocations of variables inside loops etc. that well, but I don't think that's a problem nowadays anymore.
For none arrays variables I tend to use the 2nd example to save code lines.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
FaTony
Veteran Member
Join Date: Aug 2008
Old 01-19-2010 , 04:55   Re: Use NEW or not?
Reply With Quote #3

It also matter if you're have some if's and return's before the first call of variable. Why would you allocate memory for what may never be used?

PHP Code:
some code
if (!blahblah) {
   return;
   }
new 
variable=blahblah
FaTony is offline
Dygear
SourceMod Donor
Join Date: Apr 2004
Location: Levittown, NY
Old 01-19-2010 , 05:55   Re: Use NEW or not?
Reply With Quote #4

In pawn, not source:pawn, you have no choice. When you make a new variable you must use new, that token must be created with the new keyword before you can use said token.

New allows you to make a new variable, and if you don't set a value at time of creating the variable then it's automatically set to zero, there is no cost in setting the variable at creation to another value, in fact if you can, you should as this saves from making the same function call later.
__________________
Dygear is offline
Send a message via AIM to Dygear Send a message via MSN to Dygear Send a message via Skype™ to Dygear
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 18:25.


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