Posts: 2,187
Threads: 81
Joined: Aug 2011
Reputation:
0
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!
Posts: 1,113
Threads: 127
Joined: Jul 2008
Reputation:
0
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.
Posts: 2,187
Threads: 81
Joined: Aug 2011
Reputation:
0
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!
Posts: 966
Threads: 5
Joined: Jul 2011
Reputation:
0
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
Posts: 1,113
Threads: 127
Joined: Jul 2008
Reputation:
0
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
Posts: 1,113
Threads: 127
Joined: Jul 2008
Reputation:
0
@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.
Posts: 2,421
Threads: 52
Joined: Mar 2009
Reputation:
0
20.07.2013, 09:21
(
Последний раз редактировалось iggy1; 20.07.2013 в 09:28.
Причина: Removed silly comment about RAM
)
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.