Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > inline vs macro

Reply
Thread Tools

inline vs macro

 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      12-19-2006
hi guys,

Can you please suggest that in what cases should a macro be
preferred over inline function and viceversa ? Is there any case where
using a macro will be more efficient as compared to inline function ?

thanks for any help in advance ...

 
Reply With Quote
 
 
 
 
sam_cit@yahoo.co.in
Guest
Posts: n/a
 
      12-19-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hi guys,
>
> Can you please suggest that in what cases should a macro be
> preferred over inline function and viceversa ? Is there any case where
> using a macro will be more efficient as compared to inline function ?
>
> thanks for any help in advance ...


You could refer to the following link,

http://groups.google.co.in/group/com...13a697/?hl=en#

And inline functions are much better than Macro, which you will
understand if you look at the examples in the above thread.

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      12-19-2006
(E-Mail Removed) a écrit :
> hi guys,
>
> Can you please suggest that in what cases should a macro be
> preferred over inline function and viceversa ? Is there any case where
> using a macro will be more efficient as compared to inline function ?
>
> thanks for any help in advance ...
>


#define max(a,b) ((a<b)?b:a)

Defining an inline function(s) for that would be
quite difficult...

The same for min(a,b)

Other "advantages" of macros is that they can capture
local variables in the calling function:

#define myhack(a) (a+sqrt(var))

void fn(double var)
{
int a;

...
myhack(a);
...
}

You see? The argument var is implicitely passed
to the macro.

On the other hand:

An advantage of inline functions is that they
do not capture local variables in the calling
function:

inline double myhack(int a)
{
return a+sqrt(var);
// This "var" refers to a global variable var,
// NOT to a local variable. This avoids
// UNINTENTIONAL problems with local
// variables
}
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      12-19-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> Can you please suggest that in what cases should a macro be
> preferred over inline function and viceversa ?


An inline function has type safety and doesn't suffer from
double-evaluation problems.

A macro has type generic-ness and can modify its arguments.

Only one of them may meet your needs in particular cases, though in the
simplest examples they may both work equally well.

> Is there any case where using a macro will be more efficient as
> compared to inline function ?


If you can replace one with the other and preserve the caller's syntax,
there is no reason to expect one to be more efficient than the other. A
compiler should treat them the same.

Sometimes you can't replace a macro, though. Consider:

#define INC(x) (x++)

A function simply can't do the same thing. You'd have to change callers
from this:

int x=0;
INC(x);

to:

int x=0;
x = inc(x); /* assumes int inc(x) { return x+1; } */

You might also be calling the macro with varying type arguments; there
is no way to write an inline function to replace INC() above that can
handle both doubles and ints correctly.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      12-19-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> hi guys,
>
> Can you please suggest that in what cases should a macro be
> preferred over inline function and viceversa ? Is there any case where
> using a macro will be more efficient as compared to inline function ?
>
> thanks for any help in advance ...
>


#define MAXOF(x,y) ((x)>(y)?(x)y))

This macro can be used for short, int, long, float, double, or any
combination of them. To use a function would require
defining many separate functions.


--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-19-2006
(E-Mail Removed) said:

<snip>

> And inline functions are much better than Macro


Why do you think so? And what do you mean by "better"?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      12-19-2006
In article <(E-Mail Removed) om>
(E-Mail Removed) <(E-Mail Removed)> wrote:
> Can you please suggest that in what cases should a macro be
>preferred over inline function and viceversa?


The advantage of a macro is that it simply performs textual
substitution.

The disadvantage of a macro is that it simply performs textual
substitution.

Besides the other examples posted so far, macros also have access
to the preprocessor's "#" and "##" operators:

#define DEBUG_INTEGER(var) fprintf(stderr, "DEBUG: " #var " = %d\n", var)
#define DEBUG_STRING(var) fprintf(stderr, "DEBUG: " #var " = %s\n", var)
void foo(void) {
int zorg, blaggle;
char *graump;
...
DEBUG_INTEGER(zorg);
DEBUG_INTEGER(blaggle);
DEBUG_STRING(graump);
...
}
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-19-2006
jacob navia <(E-Mail Removed)> writes:
> (E-Mail Removed) a écrit :
>> hi guys,
>> Can you please suggest that in what cases should a macro be
>> preferred over inline function and viceversa ? Is there any case where
>> using a macro will be more efficient as compared to inline function ?
>> thanks for any help in advance ...
>>

>
> #define max(a,b) ((a<b)?b:a)
>
> Defining an inline function(s) for that would be
> quite difficult...

[...]

Surely you mean:

#define MAX(a, b) (((a) < (b)) ? (b) : (a))

(with an added comment warning that one of the arguments will be
evaluated twice).

One disadvantage of inline functions is that some compilers may not
support them, or may not support them correctly. Yes, they're defined
in C99, the One True Standard for the C programming language, but C99
is not universally implemented.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
dcorbit@connx.com
Guest
Posts: n/a
 
      12-20-2006

Richard Heathfield wrote:
> (E-Mail Removed) said:
>
> <snip>
>
> > And inline functions are much better than Macro

>
> Why do you think so? And what do you mean by "better"?


Superior in every way that I can imagine.
Lack of side effects due to multiple evaluation
Type safety
Less prone to undefined behavior

A function macro offers something for the lazy programmer:
A poor man's C++ template in C ... Make that a destitute man's C++
somewhat template like thingy in C. Except that they are universally
broken.

But the ability to apply that macro to any argument is one of its
largest downfalls.

In short, function macros are the result of hazardous laziness. There
is not one single instance where the correct functions would not be
superior. If your compiler does not support inline functions, it is
time to get a new compiler. If no compiler is available for your
toaster IC, then just write the code inline instead of using macros.
Because hey, who likes burnt toast in the morning.

If I had my way, min(a,b) and max(a,b) would be removed from the
standard library headers. They are not as bad as gets(), but still
completely awful.

> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999
> http://www.cpax.org.uk
> email: rjh at the above domain, - www.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-20-2006
(E-Mail Removed) said:

>
> Richard Heathfield wrote:
>> (E-Mail Removed) said:
>>
>> <snip>
>>
>> > And inline functions are much better than Macro

>>
>> Why do you think so? And what do you mean by "better"?

>
> Superior in every way that I can imagine.
> Lack of side effects due to multiple evaluation


That can be a good thing or a bad thing, depending on whether you want the
side effects.

> Type safety


That can be a good thing or a bad thing, depending on whether you want the
type safety.

> Less prone to undefined behavior


Perhaps. But if you stop people doing stupid things, it's generally at the
expense of stopping them doing clever things, too. Doug Gwyn once said
something of the kind.

<snip>

> If your compiler does not support inline functions, it is
> time to get a new compiler.


It's not for us to tell people what compilers to use.

> If I had my way, min(a,b) and max(a,b) would be removed from the
> standard library headers.


[F/X: waves magic wand] Since it is the season of gifts, Dann, your wish is
granted. Not only have I removed min and max from the standard library
headers for you, but I have applied this change retroactively, so that it
shall be as if they had *never* been part of the standard library headers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
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
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
To inline or not to inline? Alvin C++ 7 05-06-2005 03:04 PM
Function delcared inline but not defined inline Nish C Programming 4 10-08-2004 03:31 PM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments