Archived

This topic is now archived and is closed to further replies.

Pass by Reference Params vs Globals

This topic is 5044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites