PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > When to use macros.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
When to use macros.

Réponse
 
LinkBack Outils de la discussion
Vieux 12/04/2008, 08h19   #1
prashant.khade1623@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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. ?
  Réponse avec citation
Vieux 12/04/2008, 08h24   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: When to use macros.

prashant.khade1623@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.
  Réponse avec citation
Vieux 12/04/2008, 08h40   #3
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: When to use macros.

prashant.khade1623@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
  Réponse avec citation
Vieux 12/04/2008, 11h23   #4
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: When to use macros.

Richard Heathfield <rjh@see.sig.invalid> writes:

> prashant.khade1623@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.
  Réponse avec citation
Vieux 12/04/2008, 12h09   #5
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: When to use macros.

Ben Bacarisse said:

> Richard Heathfield <rjh@see.sig.invalid> writes:
>
>> prashant.khade1623@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
  Réponse avec citation
Vieux 12/04/2008, 19h47   #6
Default User
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: When to use macros.

Ian Collins wrote:

> prashant.khade1623@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
  Réponse avec citation
Vieux 13/04/2008, 00h16   #7
Herbert Rosenau
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: When to use macros.

On Sat, 12 Apr 2008 07:19:40 UTC, "prashant.khade1623@gmail.com"
<prashant.khade1623@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. ?


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!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13332 seconds with 15 queries