468,249 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

When to use macros.

HI All,

I know the clear distinction between macro and function.

I know that macro will speed up the program and using function will
reduce the size.

But how do we know when to use macro and function. ?
Jun 27 '08 #1
6 1172
pr****************@gmail.com wrote:
HI All,

I know the clear distinction between macro and function.

I know that macro will speed up the program and using function will
reduce the size.

But how do we know when to use macro and function. ?
Use a function when you can, use a macro when all else fails. Modern
compilers do a good job of inlining short functions, so function like
macros are only really useful to implement a poor man's function
overloading.

--
Ian Collins.
Jun 27 '08 #2
pr****************@gmail.com said:
HI All,

I know the clear distinction between macro and function.
(I presume you are talking about function-like macros.)
>
I know that macro will speed up the program and using function will
reduce the size.
Insofar as this is true, it is because macros are expanded inline
(increasing the size of the program with every expansion, but eliminating
the overhead of a function call). In C99, functions can be inlined too (at
the compiler's discretion).
>
But how do we know when to use macro and function. ?
If in doubt, use a function.

Macros can be powerful and convenient, but they can't do type-checking and
they run the risk of performing side-effects more than once.

Don't be religious about it - as I said, macros /can/ be very powerful when
properly used - but it is wisest to favour functions over macros in normal
usage. This will tend to lead to fewer bugs and cleaner code.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #3
Richard Heathfield <rj*@see.sig.invalidwrites:
pr****************@gmail.com said:
<snip>
>I know that macro will speed up the program and using function will
reduce the size.

Insofar as this is true, it is because macros are expanded inline
(increasing the size of the program with every expansion, but eliminating
the overhead of a function call). In C99, functions can be inlined too (at
the compiler's discretion).
Can't functions be inlined in C90 (at the compiler's discretion)?

--
Ben.
Jun 27 '08 #4
Ben Bacarisse said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>pr****************@gmail.com said:
<snip>
>>I know that macro will speed up the program and using function will
reduce the size.

Insofar as this is true, it is because macros are expanded inline
(increasing the size of the program with every expansion, but
eliminating the overhead of a function call). In C99, functions can be
inlined too (at the compiler's discretion).

Can't functions be inlined in C90 (at the compiler's discretion)?
Um, yes, of course. What I ought to have said is: "In C99, you can request
that functions be inlined".

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #5
Ian Collins wrote:
pr****************@gmail.com wrote:
HI All,

I know the clear distinction between macro and function.

I know that macro will speed up the program and using function will
reduce the size.

But how do we know when to use macro and function. ?

Use a function when you can, use a macro when all else fails. Modern
compilers do a good job of inlining short functions, so function like
macros are only really useful to implement a poor man's function
overloading.

They are also useful for switchable behavior. As an example, you can
have extra diagnostic "functions" embedded for development, then turn
them off for release code. Naturally, you could do the same thing with
#if blocks around real function calls, but that tends to look a bit
more confusing.

Brian
Jun 27 '08 #6
On Sat, 12 Apr 2008 07:19:40 UTC, "pr****************@gmail.com"
<pr****************@gmail.comwrote:
HI All,

I know the clear distinction between macro and function.

I know that macro will speed up the program and using function will
reduce the size.

But how do we know when to use macro and function. ?
On modern compilers a function that can be inlined is to prefere over
a macro because it adds type security.

As a macro is only text replacement it can very useful when the only
difference between different functions is only a single statement or
different types on the same functionality.

A macro with parameters fails miserably on having usage of side
effects like pre/post increment on its opreands, an inlided function
avoids that.

I prefere functions over macros. On other hand I write a macro instead
of a function when I have to do the same little piece of code with
different data types.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Chris Reedy | last post: by
699 posts views Thread by mike420 | last post: by
37 posts views Thread by michele.simionato | last post: by
8 posts views Thread by Michael Winter | last post: by
1 post views Thread by Rafal 'Raf256' Maj | last post: by
3 posts views Thread by Stephen Sprunk | last post: by
47 posts views Thread by Emil | last post: by
11 posts views Thread by San | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.