Raised This Month: $51 Target: $400
 12% 

An interesting thing about decl?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 07-11-2012 , 09:29   An interesting thing about decl?
Reply With Quote #1

Today i found out an interesting thing about decl, at least for me.
I saved some data in big strings and used decl and strcat for that purpose.
Somehow my strings ended up like that
Code:
catval;catval;catval
I realised that declared strings are like static variables.
I never saw this documentated anywhere or i am blind, so i think i may not be the only one who didn't knew this yet.
here is an copy & paste example
Example

The outcome is something like this
Code:
DeclaredString: xxxxxxxx
NormalString: x
Why isn't this documentated?

Yours sincerely
Impact
__________________

Last edited by Impact123; 07-11-2012 at 09:30.
Impact123 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 07-11-2012 , 09:42   Re: An interesting thing about decl?
Reply With Quote #2

Quote:
Originally Posted by Impact123 View Post
Today i found out an interesting thing about decl, at least for me.
I saved some data in big strings and used decl and strcat for that purpose.
Somehow my strings ended up like that
Code:
catval;catval;catval
I realised that declared strings are like static variables.
I never saw this documentated anywhere or i am blind, so i think i may not be the only one who didn't knew this yet.
here is an copy & paste example
Example

The outcome is something like this
Code:
DeclaredString: xxxxxxxx
NormalString: x
Why isn't this documentated?

Yours sincerely
Impact
cause usually people format the decl before printing it?

Last edited by Mitchell; 07-11-2012 at 09:42.
Mitchell is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 07-11-2012 , 09:49   Re: An interesting thing about decl?
Reply With Quote #3

It is documented... http://wiki.alliedmods.net/Optimizin...n_Local_Arrays

Quote:
NOTE: You must be very careful not to read from uninitialized variables. They will contain garbage, and you could crash the server by attempting to print or operate on garbage strings.
You should Format or put something into the string first and not expect it to be empty.
__________________
Silvers is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 07-11-2012 , 12:26   Re: An interesting thing about decl?
Reply With Quote #4

Yeah, i knew that of course.
What i meant to say is i didn't know that decl creates "global" variables.

Yours sincerely
Impact
__________________
Impact123 is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 07-11-2012 , 12:40   Re: An interesting thing about decl?
Reply With Quote #5

Yeah, apparently decl creates global (or rather static) variables. Well, back to new then.

Last edited by hleV; 07-11-2012 at 13:54.
hleV is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 07-11-2012 , 12:44   Re: An interesting thing about decl?
Reply With Quote #6

decl doesn't create a global variable.
You always have to espect that memory which didn't get intialized yet contains garbage random data.

In this case the same memory space just gets reused again.
It's not something you should rely on as it isn't guaranteed, strange things can happen if you do rely on it.


Quote:
Originally Posted by hleV View Post
Yeah, apparently decl creates global (or rather static) variables. Well, back to new then.
The purpose of using decl is to not have to initialize every single cell of the array and save ressources,
I don't know why that would have something todo with a variable being static/global
__________________
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

Last edited by berni; 07-11-2012 at 12:47.
berni is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 07-11-2012 , 13:32   Re: An interesting thing about decl?
Reply With Quote #7

As berni said, decl doesn't initialize global/static variables. Rather, it doesn't clear the variable and just reuses whatever memory space it can find. Sometimes, this will be the previous value of it, but do not rely on it as bad things can happen.
__________________
Dr. McKay 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 19:29.


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