Lost Password?

  #1 (permalink)  
Old 03-21-2008, 10:23 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Lightbulb Inline function

In this tutorial we wil see the use of inline functions.


It sometimes so happens that we have functions scattered all over the program.In this case a function call causes the program to jumps to the address of the function and come back when the function call terminates. This takes away some precious time.


The above problem can be resolved with the use of inline functions. This causes the compiler to call the code directly from the source. No new memory instruction set is created for the inline function code. This gives the compiler an option to read from the source and there is no need for the call trace to jump around. Thus they are similar to macro definitions and are as fast as macros.


Although the inline declaration in c++ is free and occurs automatically when the function is defined in the declaration, in c it is restricted by the following rules ::

1. In C, any function with internal linkage can be declared inline, but a function with external linkage is has restrictions on inline.

2. If the inline keyword is used in the function declaration, then the function definition should be present in the same translation unit.



Most of the advantage of inline functions comes from avoiding the overhead of calling an actual function. Thus there is a saving of registers and no setting up stack frames. But inline functions present a problem for debuggers and profilers, because the function is expanded at the point of call and loses its identity.


Heres how you can use the inline functions.

The general syntax of using an inline function is :
Code:
inline datatype function_name(arguments)
A general c++ code to square a number would loook like this ::
Code:
#include <iostream.h>

class inline_
{
public:
int square(int);
};

void main()
{
	inline_ i1;
	int x;
	cout <<"\nEnter a number ::";
	cin>>x;
	cout<<"\n The square is ::" << i1.square(x);
}

int ::square(int x1)
{
	return x1*x1;
}

Now using inline functions this would become::

Code:
#include <iostream.h>

class inline_
{
public:
int square(int);
};

void main()
{
	inline_ i1;
	int x;
	cout <<"\nEnter a number ::";
	cin>>x;
	cout<<"\n The square is ::" << i1.square(x);
}

int inline_::square(int x1)
{
	return x1*x1;
}
This code runs upto 30% fastert than a non inline function, the rest depending on the prcessor speed.

Now comes the strategy part. You may use inline functions at your will but keeping in mind that inline functions can take much less time to execute but they have a high memory occupancy on the run. Also the compiler has always an option to overlook your inline declaration if the code declared inline is abnormally large compared to the code size.


Inline declaration although destroys the order of evaluation, does not make the function internal. The function is still external.
__________________
God is real... unless declared an integer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-07-2008, 03:13 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 4,899
Last Blog:
Web slideshow in JavaS...
Rep Power: 42
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: Inline function

Nice one. Cheers!
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
function pointer Chinmoy C Tutorials 0 03-19-2008 12:52 AM
SecurityAudit vinay Visual Basic Programming 27 01-07-2008 12:14 PM
multi-pass preprocessing kenna C and C++ 11 08-14-2007 10:45 AM
small help in this generator function plz SamehSpiky C and C++ 3 06-22-2007 11:47 AM


All times are GMT -5. The time now is 09:10 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads