#### Archived

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

# References in inlined functions

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

## Recommended Posts

I was wondering if references have any speed increase or even decrease on inlined functions. for example is there any difference in Vector Vector::operator+(Vector &Pls){ Vector RetRef(X+Pls.X,Y+Pls.Y,Z+Pls.Z); return RetRef; } and Vector Vector::operator+(Vector Pls){ Vector RetRef(X+Pls.X,Y+Pls.Y,Z+Pls.Z); return RetRef; }

##### Share on other sites
Well, if I get what you're asking, passing a reference to and object instead of a copy of the object would be especially good in cases where the structure is larger than a reference. So pretty much any structure is better passed by reference or pointer. I just pass by value ints and doubles; I'm not sure which is faster in this case. I have even less ideas about inline functions :S
--------------------
Kings of Chaos

[edited by - n0ob on March 17, 2003 11:09:38 PM]

##### Share on other sites
I think pretty much the same rules apply for inlined functions as well

##### Share on other sites
Not sure what your examples are to do but as far as I can tell that you want to know if references have a speed difference on inlined functions. Well take the following two example functions

struct BIG_STRUCT
{
int a,b,c,d,e,f,g,h,i,j,k,l,m,n;
};

{
//add up all the values and return
}

{
//same as before
}

Calling one of these functions would result in the SAME code being generated. So there is not speed differences. However you should note that in all likely hood the AddStruct function won''t be inlined because it''s to big. Therefore the one that uses the reference opp would be faster, in calling time, and at run time.

I have no idea if I even answered what you asked but...

##### Share on other sites
quote:
Original post by RamboBones
Calling one of these functions would result in the SAME code being generated.

100% INCORRECT.

In the example with the reference, any code in the function will actually use the memory location of the original structure used when calling the function.

In the other example (non-referenced), a NEW structure will be allocated on the stack and bitwise copied (since there''s no copy ctor).

Granted, there''s likely to be NO performance difference, but be assured that the above functions will produce DIFFERENT code. It''s guaranteed by the semantics of the c++ language.

##### Share on other sites
daerid wrote
"100% INCORRECT
In the example with the reference, any code in the function will actually use the memory location of the original structure used when calling the function.

In the other example (non-referenced), a NEW structure will be allocated on the stack and bitwise copied (since there''s no copy ctor)."

If both functions are inlined, there is no stack to allocate a new structure on for the function, and both will (should) generate the same code. In the case that the function cannot be inlined, then yes, using the reference is preferable, but the OP asked about inlined functions.

##### Share on other sites
quote:
Original post by chiuyan
If both functions are inlined, there is no stack to allocate a new structure on for the function, and both will (should) generate the same code. In the case that the function cannot be inlined, then yes, using the reference is preferable, but the OP asked about inlined functions.

Inline functions will still use local stack variables if you pass by value and modify the parameters. Or make copies in registers if it fits. Passing by reference, or better, by const reference alleviates that need.

The fact that you don''t get a stack frame for the inlined function doesn''t mean no stack frame exists : the enclosing function is adjusted to take into account the local variables and parameters of the inline functions it contains.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

• 9
• 10
• 9
• 48
• 12
• ### Forum Statistics

• Total Topics
631387
• Total Posts
2999712
×