This is a discussion on What are inline functions? within the C and C++ Programming forums, part of the Software Development category; Hi, what are inline function,advantage of inline function Thanks, prasath.K...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| The point of making a function "inline" is to hint to the compiler that it is worth making some form of extra effort to call the function faster than it would otherwise - generally by substituting the code of the function into its caller. As well as eliminating the need for a call and return sequence, it might allow the compiler to perform certain optimizations between the bodies of both functions. Sometimes it is necessary for the compiler to emit a stand-alone copy of the object code for a function even though it is an inline function - for instance if it is necessary to take the address of the function, or if it can't be inlined in some particular context, or (perhaps) if optimization has been turned off. (And of course, if you use a compiler that doesn't understand "inline", you'll need a stand-alone copy of the object code so that all the calls actually work at all.) There are various ways to define inline functions; any given kind of definition might definitely emit stand-alone object code, definitely not stand-alone emit object code, or only emit stand-alone object code if it is known to be needed. Sometimes this can lead to duplication of object code, which is a potential problem for following reasons: It wastes space. It can cause pointers to what is apparently the same function to compare not equal to one another. It might reduce the effectiveness of the instruction cache. (Although inlining might do that in other ways too.) |
| |||
| Hi Prasath i guess this reply will suits your query "Inline Functions are like Normal functions.When an inline Function is invoked the code of function is inserted instead of jump to code of function. inline keyword is added to the function definition to make that particular function inline function. inline keyword is just a request to the compiler.Sometimes Compiler will ignore the request and compile the function as normal function. " |
| |||
| Inline Functions Suppose that a program frequently requires to find the absolute value of an integer quantity. For a value denoted by n, this may be expressed as: (n > 0 ? n : -n) However, instead of replicating this expression in many places in the program, it is better to define it as a function: int Abs (int n) { return n > 0 ? n : -n; } The function version has a number of advantages. First, it leads to a more readable program. Second, it is reusable. And third, it avoid undesirable side-effects when the argument is itself an expression with side-effects. The disadvantage of the function version, however, is that its frequent use can lead to a considerable performance penalty due to the overheads associated with calling a function. For example, if Abs is used within a loop which is iterated thousands of times, then it will have an impact on performance. The overhead can be avoided by defining Abs as an inline function: inline int Abs (int n) { return n > 0 ? n : -n; } The effect of this is that when Abs is called, the compiler, instead of generating code to call Abs, expands and substitutes the body of Abs in place of the call. While essentially the same computation is performed, no function call is involved and hence no stack frame is allocated. Because calls to an inline function are expanded, no trace of the function itself will be left in the compiled code. Therefore, if a function is defined inline in one file, it may not be available to other files. Consequently, inline functions are commonly placed in header files so that they can be shared. Like the register keyword, inline is a hint which the compiler is not obliged to observe. Generally, the use of inline should be restricted to simple, frequently used functions. A function which contains anything more than a couple of statements is unlikely to be a good candidate. Use of inline for excessively long and complex functions is almost certainly ignored by the compiler. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Inline Function C++ | kingmaker | C and C++ Programming | 10 | 12-17-2008 07:17 PM |
| Sql functions | itbarota | Database Support | 11 | 02-29-2008 02:08 AM |
| Inline javascript Tips & Tricks | Sabari | HTML, CSS and Javascript Coding Techniques | 9 | 12-18-2007 06:14 AM |
| What are Virtual Functions? How to implement virtual functions in "C"? | Sabari | C and C++ Programming | 4 | 09-10-2007 11:35 PM |
| Diff inline function and ordinary function | vigneshgets | C and C++ Programming | 1 | 05-24-2007 11:34 AM |