Re: What does f(x++) mean?
From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 10/03/03
- Next message: Joe Wright: "Re: doubt in USING POINTERS"
- Previous message: Mike Wahler: "Re: [OT, spam] Backup your projects neatly"
- In reply to: becte: "Re: What does f(x++) mean?"
- Next in thread: becte: "Re: What does f(x++) mean?"
- Reply: becte: "Re: What does f(x++) mean?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 3 Oct 2003 16:47:46 -0400 (EDT)
On Fri, 3 Oct 2003, becte wrote:
>
> 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));
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.
> In AIX (xlC) this works OK, but in Linux(gcc) the list is not "packed"
> but noOfRoutePoints and routePointElementIndexWork are decreased.
Yup. Undefined behavior will do that to you.
> 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--;
That works.
> 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.
It's undefined behavior; anything can happen.
Your fixed code is fine. Go with it. (Should've been
written that way from the start.)
-Arthur
- Next message: Joe Wright: "Re: doubt in USING POINTERS"
- Previous message: Mike Wahler: "Re: [OT, spam] Backup your projects neatly"
- In reply to: becte: "Re: What does f(x++) mean?"
- Next in thread: becte: "Re: What does f(x++) mean?"
- Reply: becte: "Re: What does f(x++) mean?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|