X
Xenos
Where is the undefined behaviour?
I believe he was considering macros that make multiple accesses to a
parameter within a single sequence point.
DrX
Where is the undefined behaviour?
becte said:What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
3. undefined behavior, it is different for different compilers
I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.
What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
3. undefined behavior, it is different for different compilers
I encountered the problem when I was porting code (not written by me)
from AIX to Linux. Visual age and gcc gave different results (2 and 1
respectively), so I had to manually change f(x++); to f(x); x++
in a lot of places because f(x++) was frequently used.
From: research!dmr
Date: Mon Dec 27 06:19:21 1982
Subject: Pure and applied C
John Levine wondered whether, in the call bar(x++) with x global,
bar would see the old or the incremented value of x. The parameter
is clearly the old value; the question is whether the compiler might
be allowed to do the increment after the call. ...
Steve Johnson brought up this very
point when he was writing PCC, and I said that as far as I knew he was
entitled to delay the increment. We agreed, though, that the result
might surprise people, and he decided not to rock the boat.
becte said:What does the standard say about f(x++) ?
1. x is incremented and then the that value is
used in function f, i.e. x++; f(x);
2. the value of x (before ++) is send to f and
after returning. x is increased, i.e f(x); x++;
3. undefined behavior, it is different for different compilers
Noah Roberts said:becte said:What does the standard say about f(x++) ? [snip]
3. undefined behavior, it is different for different compilers
This one if f is a macro.
Thanks for the answers. Maybe I was oversimplifying a little bit.
The actual code looked like this
// gcc seems to be "wrong" here
listElement_s *listElement = &listElement[index];
// ...
memmove ((char *) listElement,
(char *) &list[index + 1],
sizeof (listElement_s) *
(noOfElements-- - index-- - 1));
In AIX (xlC) this works OK, but in Linux(gcc) the list is not "packed"
but noOfRoutePoints and routePointElementIndexWork are decreased.
To make it work in Linux I had to rewrite the code:
memmove ((char *) listElement,
(char *) &list[index + 1],
sizeof (listElement_s) *
(noOfElements - index - 1));
noOfElements--;
index--;
I assumed that the problem was that the decremented value of index
was used in memmove, so that index + 1 --> index. Then the
first and second arguments are equal and the list is not changed at all.
Simon Biber said:Noah Roberts said:becte said:What does the standard say about f(x++) ? [snip]
3. undefined behavior, it is different for different compilers
This one if f is a macro.
Undefined behaviour if f is a macro?
#define f(i) printf("%d\n", i)
f(x++);
Where is the undefined behaviour?
Arthur J. O'Dwyer said:This argument by itself is okay; but in combination with
the other arguments, which also try to use the value of
'index', it invokes undefined behavior. IOW, it's perfectly
well-defined and okay to write
foo(x++)
but it's entirely wrong and bad to write
foo(x, x++)
or
foo(x++, x)
or any combination of x++ and x without a sequence point
in between them. The ',' separator between function arguments
is *not* a sequence point, even though the comma operator (also
',', but in a different context) *is* a sequence point.
^^^^^^^^^^^becte said:"Arthur J. O'Dwyer" <[email protected]> wrote in message
This answers my question.
I wrote a small test program:
#include <stdio.h>
int i = 10;
int f(int a, int b)
{
printf ("a = %d, b = %d, Global i = %d\n", a, b, i);
return 0;
}
int main()
{
f(i, i--);
return 0;
}
In AIX (xlC) I get
a = 10, b = 10, Global i = 9
In Linux (gcc) I get
a = 9, b = 10, Global i = 9
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.