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

declaring static as global crashes the server if used in a function outside the file?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Natsheh
Veteran Member
Join Date: Sep 2012
Old 04-02-2021 , 18:40   declaring static as global crashes the server if used in a function outside the file?
Reply With Quote #1

this following code will crash the server potentially....

PHP Code:

static blah[32] = "player";

public 
plugin_precache()
{
      
copy(blah31"test");
      
precache_model(blah);

why is that?

i know if a variabled declared as static global the variable scope will be inside the file only. but if it used such way does it means it will be used outside the file scope?
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-02-2021 , 18:45   Re: declaring static as global crashes the server if used in a function outside the f
Reply With Quote #2

The variable name or the variable binding is file-local, not its value.
__________________
klippy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 04-03-2021 , 16:14   Re: declaring static as global crashes the server if used in a function outside the f
Reply With Quote #3

so what if i tried to insert a value from another plugin into the variable ? should it throw an error atleast?
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-04-2021 , 07:16   Re: declaring static as global crashes the server if used in a function outside the f
Reply With Quote #4

No. Symbol (variable name, etc) scope has an effect only on where you can name it from. Think about local variables:
Code:
someFunction() {
    const x = 5;
    print_server("%d", x);
}
x in this case is pointing to some value in memory and is scoped to this function, i.e. you can't use it ("name" it) from outside someFunction. Yet, you can send its value to AMXX, other plugins etc through natives. Same goes for global static variables, but their scope is the file they are defined in, not some function. File scoping is useful for include files, where you need some global data but don't want to expose it to plugins that include it, or for large plugins that are split into multiple files.
__________________

Last edited by klippy; 04-04-2021 at 07:19.
klippy is offline
mlibre
Veteran Member
Join Date: Nov 2015
Location: return PLUGIN_CONTINUE
Old 04-04-2021 , 16:31   Re: declaring static as global crashes the server if used in a function outside the f
Reply With Quote #5

a crash prevention measure is to check the existence of the model before precache
__________________
mlibre is offline
Reply


Thread Tools
Display Modes

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 02:57.


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