Skip navigation.
 
mlRe: GCC not doing the correct thing?
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


Related mailsAuthorDate
mlGCC not doing the correct thing? Andrew Yager Nov 7, 21:53
mlRe: GCC not doing the correct thing? Matthew Fahrenbach… Nov 7, 22:29
mlRe: GCC not doing the correct thing? Cameron Hayne Nov 7, 22:41
mlRe: GCC not doing the correct thing? Stéphane Sudre Nov 8, 02:35
mlRe: GCC not doing the correct thing? Richard Cleis Nov 8, 10:46
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 8, 10:53
mlRe: GCC not doing the correct thing? Richard Cleis Nov 8, 12:40
mlRe: GCC not doing the correct thing? Domain Administrat… Nov 8, 12:54
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 8, 13:08
mlRe: GCC not doing the correct thing? Nat! Nov 8, 20:23
mlRe: GCC not doing the correct thing? Andrew Yager Nov 8, 20:29
mlRe: GCC not doing the correct thing? Domain Administrat… Nov 9, 22:35
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 07:13
mlRe: GCC not doing the correct thing? Thomas Castiglione Nov 11, 13:21
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 15:21
mlRe: GCC not doing the correct thing? Keith Ray Nov 11, 15:43
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 15:46
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 16:12
mlRe: GCC not doing the correct thing? Cameron Hayne Nov 11, 23:31
mlRe: GCC not doing the correct thing? Thomas Castiglione Nov 12, 11:48