You are here:

C++/Inline function mechanism

Advertisement


Question
Sir,
Inline function codes are copied to calling function.Hence no need to call the function during execution.
doubt
1>how can a function body can be placed with in another function?
2>When(preprocessing,compiling,assembling,linking) inline function is expaned and implemented?
3>Please elaborate when inline decrease performance?

Thank you in advance.

Answer
I am going to give you a quick set of answers for C++ here, but you should read further articles for more information. You may need to brush up on related issues so a good start might be the Wikepedia article on inline expansion at:

   http://en.wikipedia.org/wiki/Inline_expansion

as it links to related articles should you need additional background material. Then there is the C++ specific:

   http://www.cplusplus.com/forum/articles/20600/

Which gives some guidelines for using inlining in C++ source code.

Now, I am assuming you mean inlining performed by C++ build tools and not by manual methods (e.g. copy, paste and tweak of function body to make it consistent with code at the inlined function call site, or preprocessor macros - for example the C library getc function might be implemented as a preprocessor macro). Or for that matter inlining as might be more applicable to other languages / execution environments (e.g. Java, .NET).

Note that "no need to call the function during execution" means:

   - there still need to be calls to the function in the C++ source code.

   - instructions are executed for the function (unless it optimises down to nothing at all) but not by making a call to
     and returning from a subroutine.

You should note that inlining functions should be seen as an optimisation and that specifying that a C++ compiler should inline a function definition is _always_ taken as a hint by C++ compilers. Whether a C++ compiler actually does inline a function depends on various things - for example function complexity, compilation optimisation settings etc. Note that compilers usually do no inlining if producing a build with debugging support as stepping into/out of/over functions when debugging is tricky (if not impossible) if the function has been inlined all over the place.

1>how can a function body can be placed with in another function?
-------------------------------------------------------------------------------
Err... at each point where the function to be inlined is called replace the call with code to perform operations equivalent to executing the called function without having to jump to the entry point of the function and executing the code there with its attendant overheads such as setting up arguments passed to the function, pushing the return location and required register value context onto the stack, popping such state off the stack and jumping back to the return location.

2>When(preprocessing,compiling,assembling,linking) inline function is expaned and implemented?
---------------------------------------------------------------------------------------------------------------
Depends, but assuming still that we are not using preprocessor macros that look like functions, then either during compilation or
linking. If during compilation then this would usually be when the compiler has produced some internal, intermediate representation of the source code being compiled. If during linking this might be because link time code generation was requested (sometimes known as whole program optimisation), as the linker has access to all compiled modules of the program - which would be in some intermediate form - and maybe even to library functions for which the source code is not available. As the linker can see much more of the program as whole it may be able to make inlining choices the compiler could not.

3>Please elaborate when inline decrease performance?
-------------------------------------------------------------
If you repeat code at many places rather than jumping to and from one chunk of code then it is quite likely the program is going to get bigger. This might not always be true - for simple functions the overheads of calling a function may be larger than the instructions that actually perform the function's operations - in which case inlining is going to be a good thing all round! However if larger functions are inlined and executed in many places then such repeated chunks of code will add to the program size. This may not be acceptable - for example on a small embedded system with not much memory, or may degrade performance as the increased size may cause more cache misses. Many, many moons ago when I worked with Prime minicomputers the concern for such code bloat caused by inlining (a feature added to a new edition of the Prime Fortran 77 compiler) was that it may cause increased paging to and from harddisk rather than memory cache misses!

Hope this helps.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.