Global variables
#1

Hello, so i recently received a gamemode which isn't clear to me... and the thing thats not clear is:
pawn Код:
new
    Query[1500],            string2[1100];
And these variables are global. Is that good? I didn't see anyone do that before. All feedback will be appreciated.
Reply
#2

How do you mean by are they good? Usually variables are created when they need to be used and destroyed instantly after they are used. They should be local if they need to be local, else global. Also, that size makes me puke. The wastage!
Reply
#3

by "are they good" i mean, whats better, local variables on every function or a huge global variable thats used everywhere instead.



EDIT:there's no need to track them between functions.
Reply
#4

I'd go for local variables, because if I use a global variable, more than one functions can access the global var and change it's value. Local, is, well, obviously local and kept to that scope. But sometimes, global variables are inevitable!
Reply
#5

Whichever way you decide to approach this, either allocate space on the stack in every function or use globals which should be kept in a special area called the data segment, you need to be careful.

With global arrays/variables, you could face value corruption issues (function 1 uses this array, and calls function 2 which also uses this array for something else, after function 2 execution function 1 wants to do something with the array, but it has been changed resulting in behavior that you do not seek).
With locals, you could face stack overflow when a function calls itself for example or if the chain of functions is really long (function 20, which allocates variables on stack, is called by function 19, which allocates ..., is called by function 18, ...., ... by function 17, ... function 1) since stack behaves like a last-in-first-out queue.

There are some specifics I won't go into as I'm not sure if they apply fully on PAWN and AMX, but this should give you a good idea of what you need to watch out for.

// Edit: About the original question - I do that as well. I have a few larger global arrays. And I've faced problems before due to not being careful enough :P
Reply
#6

Well, guess i'm gonna change it to local ones. How do i know that this "stack overflow" happened? Because never had I had problems called that
Reply
#7

Having a global string for 'query' would be fine, because you're generally not going to run in to the corruption issue AndreT described, because it's always like this generally:

pawn Код:
format(query, sizeof(query), "...", ...);
mysql_query(query);
Same could apply to 'string2', but it's not as safe.

As for how to know when stack overflow happens, I think you get a runtime error in the console, or it simply corrupts other parts of memory. Not sure.
Reply
#8

Stack overflow would most likely be accompanied by a crash, definitely undefined behavior and perhaps even a runtime warning. Also the compiler will warn you with a usage message (same one shows up when compiling with -d3) if it is anything too obvious.

Also, I'm not very sure myself about what to prefer when coding

// Another edit: I agree with MP2 about the arrays/variables which are used for something specific.
Reply
#9

@MP2
Regarding this. So it's okay too use a global variable for queries because they get sent immediately after being formatted? Then what's not safe with strings? Atleast if i'd use a string only for info: format,send.
Reply
#10

Avoid global variables unless they are absolutely necessary.

Are there any benefits in having those global vars? Can you use locals instead? I can't really think of any good reason to have a global array for a query myself.
Reply


Forum Jump:


Users browsing this thread: