argument evaluation

Discussion in 'C Programming' started by aarklon@gmail.com, Nov 4, 2007.

  1. Guest

    Hi all,

    what is the standard way for evaluating arguments in a C function is
    it Right to left evaluation or right to left evaluation ???
    , Nov 4, 2007
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    > Hi all,
    >
    > what is the standard way for evaluating arguments in a C function is
    > it Right to left evaluation or right to left evaluation ???

    It is undefined behavoir, so the implementation if free to it in any order.

    Bye, Jojo
    Joachim Schmitz, Nov 4, 2007
    #2
    1. Advertising

  3. gallows Guest

    On 4 Nov, 13:49, wrote:
    > Hi all,
    >
    > what is the standard way for evaluating arguments in a C function is
    > it Right to left evaluation or right to left evaluation ???


    I think there is no indication in the standard. For instance:

    func( foo(), bar() );

    You can't know if foo() will be executed before or after bar().

    s.
    gallows, Nov 4, 2007
    #3
  4. Joachim Schmitz said:

    > <> schrieb im Newsbeitrag
    > news:...
    >> Hi all,
    >>
    >> what is the standard way for evaluating arguments in a C function is
    >> it Right to left evaluation or right to left evaluation ???

    > It is undefined behavoir, so the implementation if free to it in any
    > order.


    ITYM "unspecified behaviour". If it were undefined, the implications would
    be astronomical, and all bad.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 4, 2007
    #4
  5. Flash Gordon Guest

    Joachim Schmitz wrote, On 04/11/07 12:53:
    > <> schrieb im Newsbeitrag
    > news:...
    >> Hi all,
    >>
    >> what is the standard way for evaluating arguments in a C function is
    >> it Right to left evaluation or right to left evaluation ???

    > It is undefined behavoir, so the implementation if free to it in any order.


    Undefined behaviour has a specific meaning, and this is not it. The
    order is unspecified.
    --
    Flash Gordon
    Flash Gordon, Nov 4, 2007
    #5
  6. Eric Sosman Guest

    wrote:
    > Hi all,
    >
    > what is the standard way for evaluating arguments in a C function is
    > it Right to left evaluation or right to left evaluation ???


    (You don't seem to be offering a wide range of choices ...)

    The argument expressions in a C function call are evaluated
    counter-clockwise.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 4, 2007
    #6
  7. "Richard Heathfield" <> schrieb im Newsbeitrag
    news:...
    > Joachim Schmitz said:
    >
    >> <> schrieb im Newsbeitrag
    >> news:...
    >>> Hi all,
    >>>
    >>> what is the standard way for evaluating arguments in a C function is
    >>> it Right to left evaluation or right to left evaluation ???

    >> It is undefined behavoir, so the implementation if free to it in any
    >> order.

    >
    > ITYM "unspecified behaviour". If it were undefined, the implications would
    > be astronomical, and all bad.

    Yes, you're right, of course.
    6.5.2.2-10 in n1256.pdf
    10 The order of evaluation of the function designator, the actual arguments,
    and subexpressions within the actual arguments is unspecified, but there is
    a sequence point before the actual call.
    Joachim Schmitz, Nov 4, 2007
    #7
  8. Guest

    On Nov 4, 8:51 am, Flash Gordon <> wrote:
    > Joachim Schmitz wrote, On 04/11/07 12:53:
    >
    > > <> schrieb im Newsbeitrag
    > >news:...
    > >> Hi all,

    >
    > >> what is the standard way for evaluating arguments in a C function is
    > >> it Right to left evaluation or right to left evaluation ???

    > > It is undefined behavoir, so the implementation if free to it in any order.

    >
    > Undefined behaviour has a specific meaning, and this is not it. The
    > order is unspecified.
    > --
    > Flash Gordon


    then what about gcc compiler, what order is it following??
    , Nov 4, 2007
    #8
  9. santosh Guest

    wrote:

    > On Nov 4, 8:51 am, Flash Gordon <> wrote:
    >> Joachim Schmitz wrote, On 04/11/07 12:53:
    >>
    >> > <> schrieb im Newsbeitrag
    >> >news:...
    >> >> Hi all,

    >>
    >> >> what is the standard way for evaluating arguments in a C function
    >> >> is it Right to left evaluation or right to left evaluation ???
    >> > It is undefined behavoir, so the implementation if free to it in
    >> > any order.

    >>
    >> Undefined behaviour has a specific meaning, and this is not it. The
    >> order is unspecified.


    >
    > then what about gcc compiler, what order is it following??


    <http://gcc.gnu.org/onlinedocs/>
    santosh, Nov 4, 2007
    #9
  10. Flash Gordon Guest

    wrote, On 04/11/07 17:47:
    > On Nov 4, 8:51 am, Flash Gordon <> wrote:
    >> Joachim Schmitz wrote, On 04/11/07 12:53:
    >>
    >>> <> schrieb im Newsbeitrag
    >>> news:...
    >>>> Hi all,
    >>>> what is the standard way for evaluating arguments in a C function is
    >>>> it Right to left evaluation or right to left evaluation ???
    >>> It is undefined behavoir, so the implementation if free to it in any order.

    >> Undefined behaviour has a specific meaning, and this is not it. The
    >> order is unspecified.
    >> --
    >> Flash Gordon

    >
    > then what about gcc compiler, what order is it following??


    Whatever it feels like on the particular system it is compiling for with
    the optimisation level selected. It is not even required to document
    what that order is or keep it consistent.
    --
    Flash Gordon
    Flash Gordon, Nov 4, 2007
    #10
  11. Eric Sosman Guest

    Flash Gordon wrote:
    > wrote, On 04/11/07 17:47:
    >> On Nov 4, 8:51 am, Flash Gordon <> wrote:
    >>> Joachim Schmitz wrote, On 04/11/07 12:53:
    >>>
    >>>> <> schrieb im Newsbeitrag
    >>>> news:...
    >>>>> Hi all,
    >>>>> what is the standard way for evaluating arguments in a C function is
    >>>>> it Right to left evaluation or right to left evaluation ???
    >>>> It is undefined behavoir, so the implementation if free to it in any
    >>>> order.
    >>> Undefined behaviour has a specific meaning, and this is not it. The
    >>> order is unspecified.
    >>> --
    >>> Flash Gordon

    >>
    >> then what about gcc compiler, what order is it following??

    >
    > Whatever it feels like on the particular system it is compiling for with
    > the optimisation level selected. It is not even required to document
    > what that order is or keep it consistent.


    For example, in a function call like

    f(a*x+y, a*x+z);

    .... the optimizer may very well arrange to evaluate the common
    sub-expression just once, producing something that might be
    written as

    t = a*x;
    f(t+y, t+z);

    If so, *neither* argument expression is evaluated "before" or
    "after" the other; their evaluations are intermixed.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 4, 2007
    #11
  12. writes:
    > On Nov 4, 8:51 am, Flash Gordon <> wrote:
    >> Joachim Schmitz wrote, On 04/11/07 12:53:
    >> > <> schrieb im Newsbeitrag
    >> >news:...
    >> >> what is the standard way for evaluating arguments in a C
    >> >> function is it Right to left evaluation or right to left
    >> >> evaluation ???
    >> >
    >> > It is undefined behavoir, so the implementation if free to it in
    >> > any order.

    >>
    >> Undefined behaviour has a specific meaning, and this is not it. The
    >> order is unspecified.

    >
    > then what about gcc compiler, what order is it following??


    Why do you care? You should never write code whose behavior depends
    on the order in which arguments are evaluated. Such code is likely to
    "work" on the system for which you write it, and fail (subtly or
    catastrophically) when you try to compile it on some other system, or
    on a newer version of the same compiler, or on the same compiler with
    different otimization options, or during a full moon.

    The compiler is free to rearrange code between consecutive sequence
    points. Since there are no sequence points between argument
    evaluations in a single function call, the compiler is free to
    rearrange the argument evaluations in any way it likes. If you need a
    specific order of evaluation, you need to introduce sequence points.
    For example, this:

    foo(expr_with_side_effects, expr_with_more_side_effects);

    is dangerous, but this:

    tmp1 = expr_with_side_effects;
    tmp2 = expr_with_more_side_effects;
    foo(tmp1, tmp2);

    is likely to be safe (if you haven't made any other errors). And
    since you're going to use more meaningful names that "tmp1" and
    "tmp2", it's likely to be easier to read and maintain.

    If that doesn't solve your problem, tell us what you're trying to
    accomplish, and we can probably help.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 4, 2007
    #12
  13. Chris Dollin Guest

    wrote:

    > what is the standard way for evaluating arguments in a C function is
    > it Right to left evaluation or right to left evaluation ???


    No. The Standard way is "any order the compiler likes"; as the programmer,
    this means that you must write your calls so that this variability /does
    not matter/.

    Whatever the arguments against this choice of ordering -- and there are
    such arguments, as well as arguments in favour -- don't matter; that's
    what you're given.

    --
    Chris "'spect my preference is obvious by now" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Nov 5, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Coghlan
    Replies:
    8
    Views:
    400
    Carl Banks
    Mar 10, 2005
  2. Ilias Lazaridis
    Replies:
    2
    Views:
    390
    Ilias Lazaridis
    Apr 24, 2005
  3. Replies:
    4
    Views:
    407
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    752
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    333
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page