Function as macro parameter

A

Andrew

Hello,

Is it bad practice to use a function as a parameter of a macro? Will
the compiler create a full copy of the function's machine code for
each invocation of the macro? Or does it create some type of pointer
to the function? Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!
 
A

Aggro

Andrew said:
to the function? Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!

Some people think the code looks better if there is return after each
function. (So they can see the end of a function right away.)

No other benefits than that.
 
I

Irrwahn Grausewitz

Hello,

Is it bad practice to use a function as a parameter of a macro? Will
the compiler create a full copy of the function's machine code for
each invocation of the macro? Or does it create some type of pointer
to the function?

A macro is handled by the C preprocessor. The C preprocessor knows
nothing about machine code or pointers - almost all it does is pure
text replacement (well, outside string literals, that is).
Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!

You do not gain much in placing a return statement at the end of
a function returning void, execpt perhaps better readability -
it depends mostly on presonal preference.
 
P

Paul Richards

Andrew said:
Hello,

Is it bad practice to use a function as a parameter of a macro? Will
the compiler create a full copy of the function's machine code for
each invocation of the macro? Or does it create some type of pointer
to the function? Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!

Macros are not real functions in that they are completely removed before
your code even makes it to the compiler.

Calling a macro is identical to if you placed the body of the macro at
the macro call site, so no 'copy' of the function is ever made.

PS. Even passing a function as a parameter into a normal function
doesn't incur any copy overhead. Referencing a function by name simply
gives you a pointer to it, so you are essentially passing only a pointer
to the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top