GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 196 of 203    Print  
Inline Function Advantages
What are the advantages of inline functions over macros?


  
Total Answers and Comments: 6 Last Update: August 31, 2009     Asked by: chilton 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: bhuvanaram
 
The inline function has the type check and the validation of the arguments passed whereas the macros do not validate anything.

Above answer was rated as good by the following members:
kvkrishnaprasad, MercurySystemsEngineering
March 25, 2009 02:20:14   #1  
bhuvanaram Member Since: March 2009   Contribution: 1    

RE: Inline Function Advantages
The inline function has the type check and the validation of the arguments passed whereas the macros do not validate anything.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
March 29, 2009 06:02:55   #2  
sirisampada.vinod2010 Member Since: March 2009   Contribution: 2    

RE: Inline Function Advantages
It is used reduce the time complexity of a program.
 
Is this answer useful? Yes | No
April 14, 2009 11:19:02   #3  
dipuprits Member Since: April 2009   Contribution: 3    

RE: Inline Function Advantages

To add to the above points Inline functions can be overloaded but Macros
cannot be overloaded.


Ex:

#define MYMAC(p) if(p) cout << "single pointer valid" << endl;

#define MYMAC(p h) if(h 1) cout << "pinter and H valid" << endl;


The second version replaces the macro. So a call to MYMAC(ptr) where ptr is
some pointer you get warnings that not enough arguments for the macro which is
because the second version has replaced the MYMAC definition.



 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
June 16, 2009 04:47:49   #4  
amarnathsatrawala Member Since: June 2009   Contribution: 3    

RE: Inline Function Advantages
Inline functions are not always advantageous.

They are appropriate when their impact on code size in not adverse i.e. their size is small and they are not called from too many different places.

They are suitable when they are in the deepest of the loop hierarchies. It helps remove too many fuction calls and improve performance. It may also help in better code optimizations.

The other advantages have been covered above like compiler support for type checking and facility of providing overloads.

 
Is this answer useful? Yes | No
July 16, 2009 01:33:06   #5  
AnisHasan Member Since: July 2009   Contribution: 2    

RE: Inline Function Advantages
Beside the adventages which are mentioned there are some more advantages of Inline Functions over Macros

1> Macro call is resolve at the compile time and compiler replce Identifier with charecter sequence so it is tough to understand error
Ex
#define ASPECT_RATIO 1.653

error message may refer to 1.653 not ASPECT_RATIO.

2> Some time operators get incremented two times in case of macros

#define max(a b) ((a) > (b) ? (a) : (b))

max(++a b); // a is incremented twice
((++a)>(b) ? (++a) :(b))



 
Is this answer useful? Yes | No
August 28, 2009 10:20:03   #6  
ashwin2090 Member Since: August 2009   Contribution: 1    

RE: Inline Function Advantages
Inline function very much confused to use it or not.
Well I want to just tell you that it has large number of disadvantages than advantages

Inline function works faster than normal function. They are included in the code where the function call appears.
The main disadvantage of inline function is the memory collapse as the function are included every time where the function call appears.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape