Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

Using a variable in a function

This topic is 4566 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

If I have a function that has it's own variable like this: void Function() { int i = 5; ... } Then does the function keep on making lots of copies of i (bad), or only one (good) ? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by daniel_i_l
If I have a function that has it's own variable like this:
void Function()
{
int i = 5;
...
}
Then does the function keep on making lots of copies of i (bad), or only one (good) ?
Thanks.
It makes 'lots of copies', but it's not 'bad'. They're created on the stack; that's part of what the stack is for, so it's really not a problem.

Sometimes if I have a local variable which allocates memory or is non-trivial to create, I make it static. I don't know whether this is good practice (someone else could probably tell you), but in any case for built-in or simple types it's not something you need to worry about.

Share this post


Link to post
Share on other sites
It will create the variable each time the function is called but then destroy it again each time the function exits. Also, for a simple type like an int, "creating" and "destroying" it really translates as moving the stack pointer a few bytes up and down so is about as trivial an operation as you can get.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Sometimes if I have a local variable which allocates memory or is non-trivial to create, I make it static. I don't know whether this is good practice (someone else could probably tell you), but in any case for built-in or simple types it's not something you need to worry about.


Personally I do the same thing, although if the language is C++, and the variable is non-trivial, I'd certainly consider making it a member of the class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
...although if the language is C++, and the variable is non-trivial, I'd certainly consider making it a member of the class.
It's never anything that 'deserves' to be a class member; most often it's something like a temporary string used to assemble text for display. Also, making it a class member could mean many instances as opposed to one (although I suppose it could be made a static class member).

Again though, it may very well be bad practice; I'm not sure.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Sometimes if I have a local variable which allocates memory or is non-trivial to create, I make it static.

Which breaks your code once you have multiple threads.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
Which breaks your code once you have multiple threads.
My projects thus far have been single-threaded; I'm sure the above example isn't the only thing in my code that isn't thread-safe. But for now at least it's not a priority for me.

Still, a good heads-up for those reading this thread.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
Quote:
Original post by jyk
Sometimes if I have a local variable which allocates memory or is non-trivial to create, I make it static.

Which breaks your code once you have multiple threads.

Fortunately, such breaks are among the easier kinds to fix, with such tools as boost::thread_specific_ptr

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!