inline function

25
Inline Function

Upload: techmx

Post on 17-Dec-2014

2.687 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

  • 1. Inline function. Workings of Inline Why need of Inline. Implementation in C++ with code example. Advantage against Macros. Where can be implemented.

2. What is the use of functionIt is used to reduce the save the memory space when afunction is likely to be called many times. 3. Everytime a function is called, it take a lotof extra time in executing a series ofinstructions. In the following ways of execution it takesmore time 1. Jumping to the function 2.Saving in Register. 3.Pushing arguments into the stack. 4.Returning to the calling function. 4. Inline functions can be implemented using the keyword inline. The inline is a request to the compiler. The function always can not be inline by the compiler The complicated functions will not be inline. Just like a macro, but different from macro. 5. C++ proposes a new feature called inlinefunctions. By using this we can eliminate the cost ofcalls to small functions. An inline function is a function that inexpanded in line when it is invoked. Inline function must be defined before theyare called. Inline keyword sends a request, not acommand to the compiler. 6. Inline function-header{function body} 7. The inline function is just a code replacement instead of thefunction call. There is a need of stack storage and other special mechanism forfunction call and return. The stack storage is used to store the return value and returnaddress for function call and return process. And while passing the parameter the stack storage needed tostore the parameter values, and from the stack area the valuesmoved to data area. 8. While using the inline function, there is no need of theseoperations, because of code replacement. The compiler can do inline on either a high-level intermediaterepresentation or a low-level intermediate representation. In either case, the compiler simply computes the arguments,stores them in variables corresponding to the functionsarguments, and then inserts the body of the function at the callsite. 9. In an application, if the time complexity is to be reducedmeans the inline function can be used. The overhead of calling and returning from the function,parameter manipulation (push/ pop) are reduced. Increases locality of references by utilizing instructioncache. After inline the compiler may perform the optimization ofthe code by boycott the dead codes. i.e., the unused codelines that will always get true or false or not performingany modification in execution. 10. Example: int pred(int x) { if (x == 0) return 0; else return x - 1; }Before inline int f(int y) { return pred(y) + pred(0) + pred(y+1); }After inlineint f(int y){int temp = 0;if (y == 0) temp += 0; else temp += y - 1; /*1*/if (0 == 0) temp += 0; else temp += 0 - 1; /*2*/if (y+1 == 0) temp += 0; else temp += (y + 1) - 1; /*3*/return temp;} 11. The temp += 0 statements in the lines marked (1), (2) and (3)do nothing. The compiler can remove them. The condition 0 == 0 is always true, so the compiler canreplace the line marked (2) with the consequent, temp += 0(which does nothing). The compiler can rewrite the condition y+1 == 0 to y == -1. The compiler can reduce the expression (y + 1) - 1 to y(assuming wraparound overflow semantics) The expressions y and y+1 cannot both equal zero. This lets thecompiler eliminate one test. These are all the actions the compiler may do for the betterresulting of the inline function implementation 12. In C++ the function can be inline using the keyword inline. The member function of a class and the global functions arealso possible to declare as inline. The compiler may or may not make the function as an inlinewe requested. It is up to the compiler. The advanced provisions made in Visual C++ to make afunction as inline. The pragmas implemented. The recursive functions also may be implemented as inline inVC++. 13. #includeint main()#include {class samp samp k;{clrscr(); int h;k.display(); public: cout