undefined behavior?

Discussion in 'C Programming' started by Loic Domaigne, Oct 29, 2007.

  1. Hello everybody,

    Let's assume that I have the following prototype:
    void myf(int x, ...) ;

    The function myf() is then called as follows:

    int i=0;
    f(a[i++],a[i++]); // a is an array

    This sounds to me like an undefined behavior, am I right?

    Thanks in advance,
    Loic.
    --
     
    Loic Domaigne, Oct 29, 2007
    #1
    1. Advertisements

  2. yes , order of evaluation in function parameters is compiler dependent
     
    mail2sandeepnl, Oct 29, 2007
    #2
    1. Advertisements

  3. Loic Domaigne

    Richard Bos Guest

    It's worse than that. It's not just implementation-defined behaviour,
    it's full-blown _un_-defined behaviour. If it only depended on the order
    of evaluation, you'd have two possible outcomes: one way 'round, or the
    other way 'round. But it's undefined, so anything may happen, from what
    the naive programmer believes "should" happen to a complete crash, and
    worse.
    The prototype of the function doesn't matter here, BTW. You modify the
    value of i twice between sequence points. _That_ causes UB, no matter
    what else.

    Richard
     
    Richard Bos, Oct 29, 2007
    #3
  4. the comma doesn't act as a sequence point here inside an argument list, like
    is otherwise would?

    Bye, Jojo
     
    Joachim Schmitz, Oct 29, 2007
    #4
  5. Loic Domaigne

    Richard Bos Guest

    The comma only introduces a sequence point when it's the comma
    _operator_. These are separators, a different part of the grammar
    entirely. So are the commas in, for example, this declaration:

    int i,j, *ip, ia[14];

    Richard
     
    Richard Bos, Oct 29, 2007
    #5
  6. Loic Domaigne

    santosh Guest

    No.
     
    santosh, Oct 29, 2007
    #6
  7. Thanks everybody for your answers.

    Cheers,
    Loic.
     
    Loic Domaigne, Oct 29, 2007
    #7
  8. (Both) true as stated. However, a 'toplevel' comma in a declaration
    coincides with either the end of a full-declarator (as here) or of an
    initializer, and either of those IS a sequence point.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
     
    David Thompson, Nov 12, 2007
    #8
    1. Advertisements

Ask a Question

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 (here). After that, you can post your question and our members will help you out.