Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the downside of using the preprocessor to define a function call?

I would like to know what the cons are of using the preprocessor in such a way:

#define SOME_FUNCTION someFunction(someArgument)

Basically I feel like this is wrong (or certainly not a best practice) - but I'm not sure why... my preprocessor skills are rusty at best.

like image 691
Heckmaier Avatar asked Jan 18 '10 16:01

Heckmaier


2 Answers

A downside? Usually a macro definition doesn't end up in the executable's symbol table. Slightly more difficult to debug.

like image 180
Richard Pennington Avatar answered Oct 05 '22 23:10

Richard Pennington


The problem is that the arguments are re=evaluated each time they are used:

#define MIN(A,B)   ((A) < (B))?(A):(B);

Notice that I have to wrap all the arguments in '(' ')' to make sure the expression evaluates corectly. But what happens if we do this?

int  s = MIN(++current,Max);

Coding this I would expect current to be incremented once before the function is called. But because it is a macro it is incremented once in the test and a second time if it is still smaller than Max

like image 44
Martin York Avatar answered Oct 05 '22 23:10

Martin York