Sunday, August 2, 2009

Inline functions in VC++ (Visual studio 6)?

I was told that Inline functions in C++ work like macros in C


saving the 'overheads' before and after function calls.


But Disassembly window in VC++ shows other wise. Here is the code..





inline double InlSquare(double x){return(x*x);} //inline


#define MacSquare(x) (x)*(x) //Macro





int main(void){double a=2.5,b=0,c=0;


b=InlSquare(a);


c=MacSquare(a);


return(0);}





Here is an excerpt from VC++ Disassembly with 'Source annotation'-


9: b=InlSquare(a); // inline Function


mov eax,dword ptr [ebp-4]


push eax


mov ecx,dword ptr [ebp-8]


push ecx


call @ILT+10(InlSquare) (0040100f)


add esp,8


fstp qword ptr [ebp-10h]





10: c=MacSquare(a); // Macro Function


fld qword ptr [ebp-8]


fmul qword ptr [ebp-8]


fstp qword ptr [ebp-18h]





After the "Call @ILT+10" instruction we encounter push and pops


push ebp


mov ebp,esp


sub esp,40h


and many more..'over heads' before the execution of fld,fmul and fstp etc.





Is there any way to make inlines work like C Macros ?

Inline functions in VC++ (Visual studio 6)?
The link below would have the answers you need.





To summarise it for you, VC does have a way of forcing inline functions to be like macro that is '__forceinline'





I do not know why you want to force the inline functions to work like macros, but take a read at the link below and see why it's not recommended to use __forceinline.





Hope this helps.


No comments:

Post a Comment