Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > inline function vs function-like macro

Reply
Thread Tools

inline function vs function-like macro

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      03-06-2007
Suppose I am using a compiler which is C99 compliant.

Function-like macros have the disadvantage if an argument with side-
effects is passed.
For example,

#define SQUARE(x) ( (x) * (x) )

If SQUARE(x++) is called, it results in undefined behaviour.

Instead we can use the C99 feature of inline function. Am I correct ?

Given this, is there any reason for still preferring function-like
macros over inline function feature of C99 ?

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-06-2007

http://www.velocityreviews.com/forums/(E-Mail Removed), India wrote:
> Suppose I am using a compiler which is C99 compliant.
>
> Function-like macros have the disadvantage if an argument with side-
> effects is passed.
> For example,
>
> #define SQUARE(x) ( (x) * (x) )
>
> If SQUARE(x++) is called, it results in undefined behaviour.
>
> Instead we can use the C99 feature of inline function. Am I correct ?
>
> Given this, is there any reason for still preferring function-like
> macros over inline function feature of C99 ?


Yes. Macros allow use without regard to type checking. Because they're
a compile-time feature, they can be used to do certain things that
would not be possible with functions. But more importantly, inline is
a C99 feature and is not very portable across a wide range of
compilers.

Also inline specifies to the compiler to make access to the function
as fast as possible, but doesn't actually require it to embed the code
for the function, wherever it's called, unlike with macros.

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      03-06-2007
(E-Mail Removed), India wrote, On 06/03/07 03:26:
> Suppose I am using a compiler which is C99 compliant.
>
> Function-like macros have the disadvantage if an argument with side-
> effects is passed.
> For example,
>
> #define SQUARE(x) ( (x) * (x) )
>
> If SQUARE(x++) is called, it results in undefined behaviour.
>
> Instead we can use the C99 feature of inline function. Am I correct ?


Yes, given certain limitations.

> Given this, is there any reason for still preferring function-like
> macros over inline function feature of C99 ?


Yes, in certain situations. Taking your example, you can pass an int,
double or any other numeric type to SQUARE and it will "do the right
thing"(tm). However, if you use inline functions and you want to be able
to use it on all types without converting ints to doubles, or even a
complex type you will have to write squarei, squarel, squared, squarecd
etc. and call the correct one. Not always an issue, but a point to bare
in mind.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inline function or macro? jacob navia C Programming 16 01-03-2010 11:57 AM
How to know the inline-function was implemented the inline way or normal way? ypjofficial@indiatimes.com C++ 7 07-18-2006 11:22 PM
forcing compiler to consider inline function inline. Ajay C++ 5 04-01-2006 02:03 PM
Use of inline function in inline function - is it allowed? TGOS C Programming 3 02-28-2005 10:49 AM
Function delcared inline but not defined inline Nish C Programming 4 10-08-2004 03:31 PM



Advertisments