Click here to Skip to main content
15,915,324 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionWhy is it bad to use a macro function? Pin
-Dy8-Apr-04 0:14
-Dy8-Apr-04 0:14 
AnswerRe: Why is it bad to use a macro function? Pin
toxcct8-Apr-04 0:25
toxcct8-Apr-04 0:25 
GeneralRe: Why is it bad to use a macro function? Pin
Ravi Bhavnani8-Apr-04 3:52
professionalRavi Bhavnani8-Apr-04 3:52 
GeneralRe: Why is it bad to use a macro function? Pin
toxcct8-Apr-04 4:09
toxcct8-Apr-04 4:09 
AnswerRe: Why is it bad to use a macro function? Pin
Maximilien8-Apr-04 0:47
Maximilien8-Apr-04 0:47 
AnswerRe: Why is it bad to use a macro function? Pin
Antony M Kancidrowski8-Apr-04 2:07
Antony M Kancidrowski8-Apr-04 2:07 
GeneralRe: Why is it bad to use a macro function? Pin
toxcct8-Apr-04 2:08
toxcct8-Apr-04 2:08 
AnswerRe: Why is it bad to use a macro function? Pin
Tim Smith8-Apr-04 4:17
Tim Smith8-Apr-04 4:17 
Here is the classic example:

#define tolower(x) (isupper(x)?(x)-32: (x))

*pOut++ = tolower(*pIn++);

What does that expand to?

*pOut++ = (isupper(*pIn++)?(*pIn++)-32: (*pIn++));

Due to the expansion, pIn will be incremented twice. This is a behavior that is totally different from what is expected when tolower is defined as a function.

Here is another example

#define DOADD5(x) x+5

int iOutput = DOADD32(1)*2;

What is the result, 12? Nope, the result is 11.

When you look at the expansion, you get 1+5*2. Do to operator precedence, you get 5*2 then add 1 instead of 1+5 then times 2.

Some of these problems can be avoided, some can not.

Tim Smith

I'm going to patent thought. I have yet to see any prior art.
AnswerRe: Why is it bad to use a macro function? Pin
Nemanja Trifunovic8-Apr-04 5:15
Nemanja Trifunovic8-Apr-04 5:15 
AnswerRe: Why is it bad to use a macro function? Pin
John R. Shaw8-Apr-04 8:26
John R. Shaw8-Apr-04 8:26 
GeneralApiHook Pin
uqtur7-Apr-04 23:55
uqtur7-Apr-04 23:55 
GeneralMicrosoft Access Help Pin
Anonymous7-Apr-04 23:27
Anonymous7-Apr-04 23:27 
Questionwhy can i only see "index,left,...."these strings i set when i set point's parameter negative? Pin
Anonymous7-Apr-04 22:54
Anonymous7-Apr-04 22:54 
AnswerRe: why can i only see "index,left,...."these strings i set when i set point's parameter negative? Pin
Roger Allen7-Apr-04 23:47
Roger Allen7-Apr-04 23:47 
GeneralUNICODE again Pin
jancsi7-Apr-04 22:29
jancsi7-Apr-04 22:29 
GeneralRe: UNICODE again Pin
grigsoft7-Apr-04 23:44
grigsoft7-Apr-04 23:44 
GeneralRe: UNICODE again Pin
grigsoft8-Apr-04 0:42
grigsoft8-Apr-04 0:42 
GeneralRe: UNICODE again Pin
jancsi8-Apr-04 5:18
jancsi8-Apr-04 5:18 
GeneralRe: UNICODE again Pin
RChin8-Apr-04 5:55
RChin8-Apr-04 5:55 
GeneralProperty Sheet Wizard Pin
erivan7-Apr-04 21:49
erivan7-Apr-04 21:49 
GeneralProblems, many problems in Visual C++ Pin
Filomela7-Apr-04 21:36
Filomela7-Apr-04 21:36 
GeneralRe: Problems, many problems in Visual C++ Pin
David Crow8-Apr-04 2:02
David Crow8-Apr-04 2:02 
GeneralMIS Pin
loveweif7-Apr-04 21:26
loveweif7-Apr-04 21:26 
GeneralRe: MIS Pin
David Crow8-Apr-04 2:04
David Crow8-Apr-04 2:04 
Questionwhat class can use "serialize()"? Pin
Anonymous7-Apr-04 21:17
Anonymous7-Apr-04 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.