NEWBIE Help needed with define MACRO

S

Student911

Hello,

I read in a lecture that the output of the following code is 5 although
we would expect it to be 4.

///////////////////////////////////////
#define POW2(x)(x*x)

int main(){
int x=3;
std::cout<<POW2(x++)<<std::endl;
}
///////////////////////////////////////

I don't understand why the output will be 5 and I don't understand why
we would expect it to be 4. What I see is that it should be 9 or 16
since its POW.
Can someone explain that to me?

Thanks
 
A

alexmdac

The value of x is 5. It's incremented twice by the code from the
expanded macro.

The order of evaluation of (x++)*(x++) is undefined. On my machine it
appears to be (x*x); x++; x++; and the value of POW(x++) is 9.

The point is that macros need to be handled with a lot of care.
They're not evil, though, just deceptive.
 
S

Student911

The value of x is 5. It's incremented twice by the code from the
expanded macro.

The order of evaluation of (x++)*(x++) is undefined. On my machine it
appears to be (x*x); x++; x++; and the value of POW(x++) is 9.

The point is that macros need to be handled with a lot of care.
They're not evil, though, just deceptive.

Why is it incremented twice by the code? As you said it should say
(x++)*(x++)
If not defined then its an erroe. I don't get where the increment
happens and why? All what macro does is replace the define with the
code.
And why would we expect 4 ?

Thanks
 
D

Dietmar Kuehl

The value of x is 5.

You are wrong: the value of 'x' and actually the behavior of the
whole program is undefined: it changes a variable ('x') twice
without intervening sequence points.
The point is that macros need to be handled with a lot of care.
They're not evil, though, just deceptive.

Your assessment is obviously uninformed (otherwise you would have
noticed that the program has undefined behavior) and actually also
wrong: macros *are* evil. You should use them where you have to
and avoid them otherwise:

| template <typename T> inline T pow2(T x) { return x * x; }

This template will do the right thing, even when being passed
fishy expressions like ones mutating their argument. There are
uses of macros but function inlining is not one of them.
 
A

alexmdac

The value of y after "x = 3; y = x++;" is 3. However, "x++" increments
x as a side-effect. Since the macro expands to two "x++"s, x is
incremented twice.

If POW2 was a regular function then x++ would be evaluated (once)
before its value was passed. This is what's meant by "call-by-value".
The function argument's value would be 3 and the value of x afterwards
would be 4.
Angry man, it's 5 on my machine :) Read the guy's original question!
 
K

Karl Heinz Buchegger

Student911 said:
Why is it incremented twice by the code? As you said it should say
(x++)*(x++)
If not defined then its an erroe.

Well. It is not exactly an error in the sense that the compiler
is required to detect it. Syntactically there is nothing wrong
with it. It is just that its semantics (what happens during
runtime) is undefined. Anything can happen.
I don't get where the increment
happens and why? All what macro does is replace the define with the
code.

Yep. Now do the substitution by yourself and you get

std::cout << x++ * x++ << std::endl;

-> 2 attempts for incrementing
And why would we expect 4 ?

Nobody would expect that. Most would expect either 9 or 16 as output.
Those who know C++ wouldn't expect anything, saying that the whole
thing is undefined and add: but almost all compiler in practice would
produce a program which outputs either 9 or 16 although none of
those numbers is guaranteed by the language standard.
 

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

Similar Threads

Help with Loop 0
Help with a #define? 3
Code help please 4
Help with my responsive home page 2
macro question 2
token concatenation macro 4
macro 4
I need help 1

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top