Raised This Month: $ Target: $400
 0% 

Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Merudo
Senior Member
Join Date: Feb 2016
Old 02-19-2016 , 16:10   Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #1

Is there any difference between declaring a boolean variable through either one of these:
Code:
new bool:myvar1;
decl bool:myvar2;
bool myvar3;
All three compile fine...
Merudo is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-19-2016 , 16:16   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #2

The first one is "old" sourcepawn syntax.

The second one is also old syntax, but since decl was specified, the data is NOT initialized to zero; this step is skipped for speed's sake. Whatever bits were in that memory range already will not be cleared unless until you modify the variable yourself later; if you were to view the data in the variable immediately after variable creation, you'd see gibberish/garbage. decl was really only used with large arrays, especially Strings.

The third is the "new" syntax equivalent of the first. The first and third should compile to the exact same binary.

New syntax has no equivalent to decl, as the developers now view decl as being unsafe and a mistake to have ever implemented in the first place.
__________________

Last edited by ddhoward; 02-19-2016 at 16:19.
ddhoward is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 02-19-2016 , 16:18   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #3

Thanks! I guess I'll read more about the new sourcepawn syntax you mentioned.

I guess this is a good start?

Last edited by Merudo; 02-19-2016 at 16:19.
Merudo is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-19-2016 , 16:21   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #4

Yes, that's exactly the place to start.

Additional information on the subject can be found here:
https://forums.alliedmods.net/showthread.php?t=244092

You can enforce "new syntax" rules on your plugin by prepending the following line near the top of your code:

Code:
#pragma newdecls required;
Most everything which is written in the old style after this line will cause a compilation failure.
__________________

Last edited by ddhoward; 02-19-2016 at 16:29.
ddhoward is offline
Godis
Senior Member
Join Date: Jan 2014
Old 02-19-2016 , 16:25   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #5

Before SourceMod 1.7 there was only
PHP Code:
new bool:variable;
decl bool:variable
But since it's release, you can now do
PHP Code:
bool variable
The reason for this is the developers want SourcePawn to become more "modern", and become more like most other C-like languages.

"decl" was usually used for strings. What it meant was the string did not have a null terminator ('\0') at the end of the array, and could contain anything (thus it could lead to a potential crash). This meant you only did this when you absolutely knew that you would in some way populate the string before using it, through a function like Format or GetClientName etc. The reason it was added was that it was thought to make performance better, but the difference is only noticeable if you had arrays that were extremely large, therefore it is not supported in the new syntax. For example, a bool created with "new" will default to false, but a bool created with "decl" has no initialization at all, and may not even have a value.

EDIT: There were no answers when i began writing this.

Last edited by Godis; 02-19-2016 at 17:52.
Godis is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 02-19-2016 , 16:27   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #6

Quote:
Originally Posted by Godis View Post

EDIT: There were no answers when i began writing this.
LOL.

Also: There is no noticeable optimization when initializing an array in local scope. Unless you are doing it in spammy callbacks like OnGameFrame or something, in which case move it to the global scope if you can.
Potato Uno is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 02-19-2016 , 16:42   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #7

Quote:
Originally Posted by ddhoward View Post
You can enforce "new syntax" rules on your plugin by prepending the following line near the top of your code:

Code:
#pragma newdecls required;
Most everything which is written in the old style after this line will cause a compilation failure.
That's neat, but apparently the sdktools are not in the new style, so I can't use this for my plugins.

EDIT: Never mind, I can just add this line after the include.

Last edited by Merudo; 02-19-2016 at 16:44.
Merudo is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-19-2016 , 16:44   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #8

Quote:
Originally Posted by Merudo View Post
That's neat, but apparently the sdktools are not in the new style, so I can't use this for my plugins.
Just put the #pragma newdecls required; line BELOW the #include <sdktools> line.

PHP Code:
//bad
#pragma newdecls required;
#include <sdktools> 

PHP Code:
//good
#include <sdktools>
#pragma newdecls required; 
__________________

Last edited by ddhoward; 02-19-2016 at 16:44.
ddhoward is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 02-19-2016 , 18:19   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #9

Cool!

Also, how do I create a vector of vector with the new syntax?

For example, new const String:mystrings[4][] had 4 "strings", each of variable length.

How do I do it in the new syntax?

const char[] mystrings[4] doesn't work.
Merudo is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 02-19-2016 , 19:15   Re: Variable declaration: "new bool:myvar", "bool myvar", or "decl bool:myvar"?
Reply With Quote #10

Remove the const qualifier and do char mystrings[4][]
Miu 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 05:08.


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