|
|
|
|
|
|
Yes. Yes. The return value can be an anonymous temporary: 1 2 3 int function( int first, int second ) { return first+ second ; } |
i = j * k ;
vs. if(pi) *pi = pj && pk ? *pj * *pk : 0 ;
|
|
i might be adding up two class objects or structs |
|
|
And in the second one, how long does the static int num stay in memory ? |
does it get deleted after the function is out of scope? |
And because it is declared within the function, it is not reachable from outside of the function, correct ? |
And finally, which one is better ? and what is the difference ? like memory allocation, and stuffs like that. |
|
|
|
|
A point of terminology: num isn't static in your example. It is local. does it get deleted after the function is out of scope? Locals get deleted when the block that contains them goes out of scope. Since the block containing num is the function, then "yes" it gets deleted when the program flow leaves the function. And because it is declared within the function, it is not reachable from outside of the function, correct ? Correct. By the way, you might think "hey, what if I return a pointer to num? Can I see it then?" Then answer is no. The memory occupied by num will get reused for other local variables by other functions, so even if you did return a pointer to it, the pointer would point to garbage. Note also that the address of num can change from one call to the next. It truly is a variable that is created, has a lifetime and then gets destroyed. And finally, which one is better ? and what is the difference ? like memory allocation, and stuffs like that. Well you're leaking the memory in your pointer example, so that's definitely bad. But even if you didn't leak it, a local variable is usually preferable to one allocated on the heap: - Allocating a local variable often requires ZERO extra time. When you call a function, the program usually adjusts the stack pointer. If it has to make room for an extra local variable then that just changes the value that gets added/subtracted from the stack pointer. - In contrast, allocating (and later freeing) space on the heap requires a bunch of instructions. - Note that the difference here is in the time to allocate space for the variable. The time required to construct (and destroy) it will be the same in either case. So when should you put a local variable on the heap? When it's big. Programs allocate a relatively small amount of space to the stack. If you put a 5 megabyte array there, then you're likely to run out of stack space. You might want to consider this for anything larger than a kilobyte or so. |
Makes no difference. One can define addition for classes and structs just like the language has defined addition for int. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Foo { // ... public: Foo & operator+= ( const Foo & rhs ); }; Foo operator+ ( Foo lhs, const Foo & rhs ) { return (lhs += rhs); } Foo function( Foo first, Foo second, Foo third ) { return first + second + third; } |
{
So when should you put a local variable on the heap? Any local variable (whether with static or automatic storage duration) must have a name. An object with dynamic storage duration can't be given a name; it is anonymous.
|