AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   declaring static as global crashes the server if used in a function outside the file? (https://forums.alliedmods.net/showthread.php?t=331701)

Natsheh 04-02-2021 18:40

declaring static as global crashes the server if used in a function outside the file?
 
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?

klippy 04-02-2021 18:45

Re: declaring static as global crashes the server if used in a function outside the f
 
The variable name or the variable binding is file-local, not its value.

Natsheh 04-03-2021 16:14

Re: declaring static as global crashes the server if used in a function outside the f
 
so what if i tried to insert a value from another plugin into the variable ? should it throw an error atleast?

klippy 04-04-2021 07:16

Re: declaring static as global crashes the server if used in a function outside the f
 
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.

mlibre 04-04-2021 16:31

Re: declaring static as global crashes the server if used in a function outside the f
 
a crash prevention measure is to check the existence of the model before precache


All times are GMT -4. The time now is 00:58.

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