Using a variable in a function

Started by
7 comments, last by MaulingMonkey 17 years, 12 months ago
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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
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.
Thanks for the reassurement!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
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.
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.
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.
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.
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

This topic is closed to new replies.

Advertisement