Pass by Reference Params vs Globals

Started by
5 comments, last by hapaboy55 20 years, 1 month ago
Is there any difference in terms of performance between passing an object by reference and making the object global? i.e. is this:

int myFunction(AnObject & o) {...}
.
.
myFunction(myobject);
Much different performance-wise than this:

AnObject myobject;

int myFunction() {...}
.
.
myFunction();
Which would have better performance? Thanks!
Advertisement
Using globals may be faster (since you don''t need to pass an address on the stack), but if so, then by so minuscule an amount that this should not affect you; this should not be the deciding point. Pass parameters; don''t use globals.
The performance difference eiether way would be so tiny it wouldn''t matter, but using globals everywhere is extremely messy.
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
why exactly is using globals so dangerous?? besides the risk of acciedently assigning a value to it that you didnt want, what else is the problem with them? is it just read-ability problems, or something else?
FTA, my 2D futuristic action MMORPG
quote:Original post by graveyard filla
why exactly is using globals so dangerous?? besides the risk of acciedently assigning a value to it that you didnt want, what else is the problem with them? is it just read-ability problems, or something else?


Well, you don't want just any function to be able to screw with your data. Local variables can be passed between functions and you can use the same names for variables in every function if you wanted to.

And, for example: say you wanted to make an RPG, and you used a global variable for the main character's HP. Hehe, go ahead and try doing this yourself like I did, it's really really messy and confusing.



[edited by - Krak on February 20, 2004 6:48:02 PM]
quote:Original post by graveyard filla
why exactly is using globals so dangerous?? besides the risk of acciedently assigning a value to it that you didnt want, what else is the problem with them? is it just read-ability problems, or something else?

Readability is one issue. There''s also the issue of discipline (you must resist the temptation to do things with it that you shouldn''t). It''s also going to wreak havoc on your program if you ever write a multithreaded application, since global data is shared between threads.

Readability should be enough of an argument, though. Making your program more readable (and thefore, more maintainable) is well worth sacrificing that minuscule bit of potential speed that you might lose (but will never miss), especially early in the development process.
quote:
why exactly is using globals so dangerous?? besides the risk of acciedently assigning a value to it that you didnt want, what else is the problem with them? is it just read-ability problems, or something else?

Globals aren''t necassarily bad if you still modularize your code. But most people abuse globals, so that''s why they are generally not a good idea to use.
Using the HP example, say you wanted to add another player. But uh-oh, all your functions used the HP variable directly, meaning now you have to go through and pass the HP as a paramter.
One use of globals that is ok is the global string buffer (a pointer to it is returned when you call most c standard lib string functions). You never directly access it, so you don''t have to worry if the standard library suddenly uses two string buffers rather than one (for who knows what reason).

This topic is closed to new replies.

Advertisement