Re: What does f(x++) mean?

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 10/03/03


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



Relevant Pages

  • Re: Interesting coding idea
    ... C or C++ undefined behavior because of multiple updates between ... Once converted to a sequence of instructions, ... We can use static analysis in this case to determine that ... sizeof< 20, indicating a questionable construct. ...
    (comp.lang.c)
  • Re: Interesting coding idea
    ... C or C++ undefined behavior because of multiple updates between ... Once converted to a sequence of instructions, ... We can use static analysis in this case to determine that ... sizeof< 20, indicating a questionable construct. ...
    (comp.programming)
  • Re: Benefit of not defining the order of execution
    ... expressions. ... operator and with the comma operator. ... would currently result in undefined behavior. ... separators and as sequence points so that this would be equivalent: ...
    (comp.lang.c)
  • Re: Is this UB?
    ... and I'm afraid I'm still confused. ... Okay, let's do a frinstance. ... sequence point, increment p so that it points to zog, the element ... This pointer value happens to be the same value that p had at ...
    (comp.lang.c)
  • Re: ++ devils
    ... We just tried the following expressions in MSVC one by one:- ... It seems that these are problemswith 'sequence points'. ... Many of the constructs that invoke undefined behavior are just plain ...
    (comp.lang.c)