Using a variable in a function
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.
Quote:Original post by daniel_i_lIt 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.
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.
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.
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.
Quote:Original post by EndarIt'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).
...although if the language is C++, and the variable is non-trivial, I'd certainly consider making it a member of the class.
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 Fred304My 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.
Which breaks your code once you have multiple threads.
Still, a good heads-up for those reading this thread.
Quote:Original post by Fred304Quote: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
Popular Topics
Advertisement