Hi!
It's wise to declare EVERY variable as global, whether they have to be global or not? Do it costs hard-drive speed, RAM or speed?
Need global variables more space/RAM than local? (solved)
Need global variables more space/RAM than local? (solved)
Last edited by PureBaser on Sun May 21, 2006 3:29 pm, edited 1 time in total.
PB 4 | WinXP_SP2
I wouldn't have thought that there would be any speed implications, but it is definitely not wise to declare all variables as being global; unless this is a very small and simple program.
Over use of global variables is asking for trouble and inviting hard to find bugs in your programs. No, in a procedural language such as PB, the isolation of functions and procedures leads to better code reuse etc. and so they do not want to be overly reliant on global variables, particularly if the procedures are to be nested in some way.
These days I wrap up my global variables in structures. E.g. I might have a global structure for user preferences, another one for GUI etc. This way, I know that any variable being used explicitly in my code is 'local' and not global. Makes bug hunting a lot easier.
Still, I'd prefer not to make any bugs at all...
Over use of global variables is asking for trouble and inviting hard to find bugs in your programs. No, in a procedural language such as PB, the isolation of functions and procedures leads to better code reuse etc. and so they do not want to be overly reliant on global variables, particularly if the procedures are to be nested in some way.
These days I wrap up my global variables in structures. E.g. I might have a global structure for user preferences, another one for GUI etc. This way, I know that any variable being used explicitly in my code is 'local' and not global. Makes bug hunting a lot easier.
Still, I'd prefer not to make any bugs at all...

I may look like a mule, but I'm not a complete ass.
GLOBALs are usualy very slightly faster than locals but they take up a lot more space.
GLOBALs are faster because they don't need to be created and destroyed evrytime a procedure is called and they can be accessed via a simpler addressing mechanism because their location is fixed at compile time.
LOCALs need to be created and destroyed each time and, because they exist on the stack, their position changes each time so slightly more complex addressing is needed.
GLOBALS take more space because they need to exist at all times and so they are always taking up space but LOCALs only exist while the prodedure they are declared in is active and are then deleted.
It's usually not a good idea to define everything as GLOBAL. Only do that for the few variables that need it.
Paul.
GLOBALs are faster because they don't need to be created and destroyed evrytime a procedure is called and they can be accessed via a simpler addressing mechanism because their location is fixed at compile time.
LOCALs need to be created and destroyed each time and, because they exist on the stack, their position changes each time so slightly more complex addressing is needed.
GLOBALS take more space because they need to exist at all times and so they are always taking up space but LOCALs only exist while the prodedure they are declared in is active and are then deleted.
It's usually not a good idea to define everything as GLOBAL. Only do that for the few variables that need it.
Paul.
Well, believe me or not having all variable in one place is everything but not good for overview.
Better declare variables before the parts you use them(with the define statement)
Better declare variables before the parts you use them(with the define statement)
Visit www.sceneproject.org