FROM : Cameron Hayne
DATE : Thu Nov 07 22:41:02 2002
On 8/11/02 12:50 am, "Andrew Yager" <<email_removed>> wrote:
> #define cube(x) x*x*x
> [...]
> printf("%d\n", cube(*ptr++));
The result of the macro expansion is an expression which involves more than
one reference to the incremented variable. Such things have undefined
behaviour. Since the standards (C & C++) say that the compiler is free to do
whatever it wants with such expressions, GCC is definitely doing the "right
thing" whatever it does. It might be nice if it gave you a warning message
but it doesn't. By the way, this is a frequently asked question and you
might like to peruse the C FAQ at: http://www.eskimo.com/~scs/C-faq/top.html
Bottom line: avoid such constructions. Always assign complicated expressions
to a temp variable when using a macro. Or use functions instead.
--
Cameron Hayne (<email_removed>)
Hayne of Tintagel
DATE : Thu Nov 07 22:41:02 2002
On 8/11/02 12:50 am, "Andrew Yager" <<email_removed>> wrote:
> #define cube(x) x*x*x
> [...]
> printf("%d\n", cube(*ptr++));
The result of the macro expansion is an expression which involves more than
one reference to the incremented variable. Such things have undefined
behaviour. Since the standards (C & C++) say that the compiler is free to do
whatever it wants with such expressions, GCC is definitely doing the "right
thing" whatever it does. It might be nice if it gave you a warning message
but it doesn't. By the way, this is a frequently asked question and you
might like to peruse the C FAQ at: http://www.eskimo.com/~scs/C-faq/top.html
Bottom line: avoid such constructions. Always assign complicated expressions
to a temp variable when using a macro. Or use functions instead.
--
Cameron Hayne (<email_removed>)
Hayne of Tintagel






Cocoa mail archive

