|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. ? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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! |
|
![]() |
| Outils de la discussion | |
|
|