Sequence points while evaluating function arguments, Unspecified or Undefined behavio

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aftabpasha
    New Member
    • Oct 2008
    • 32

    #1

    Sequence points while evaluating function arguments, Unspecified or Undefined behavio

    In a function call like
    Code:
    printf("%d,..",++i,++i,..)
    can we consider every ++i as a Full Expression? And if it is a full expression, it means there is a sequence point after every ++i. So, uncertainty exists only about order of evaluation of arguments and not about the side-effects of each of the ++i expressions causing Undefined behavior. I just want to know whether such function calls cause Undefined Behavior or just Unspecified Behavior. If they are undefined behaviors, please give an example for better understanding of explanation.
    Thank you.

    Regards,
    Aftab
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Aftabpasha
    In a function call like
    Code:
    printf("%d,..",++i,++i,..)
    can we consider every ++i as a Full Expression? And if it is a full expression, it means there is a sequence point after every ++i. So, uncertainty exists only about order of evaluation of arguments and not about the side-effects of each of the ++i expressions causing Undefined behavior. I just want to know whether such function calls cause Undefined Behavior or just Unspecified Behavior. If they are undefined behaviors, please give an example for better understanding of explanation.
    Thank you.

    Regards,
    Aftab
    This is what the Standard has to say about sequence points:

    Originally posted by C Standard
    A.2 SEQUENCE POINTS

    The following are the sequence points described in $2.1.2.3.


    * The call to a function, after the arguments have been evaluated
    ($3.3.2.2).

    * The end of the first operand of the following operators: logical
    AND && ($3.3.13); logical OR || ($3.3.14); conditional ? ($3.3.15);
    comma , ($3.3.17).

    * The end of a full expression: an initializer ($3.5.7); the
    expression in an expression statement ($3.6.3); the controlling
    expression of a selection statement ( if or switch ) ($3.6.4); the
    controlling expression of a while or do statement ($3.6.5); the three
    expressions of a for statement ($3.6.5.3); the expression in a return
    statement ($3.6.6.4).
    Nowhere does it state that a function argument evaluation is a full expression evaluation so there is no sequence point after the evaluation of a single function argument.

    Even more: the order of function argument evaluation is unspecified, so your example most certainly causes undefined behaviour because you are modifying a modifiable lvalue more than once before a sequence point has been reached.

    kind regards,

    Jos

    Comment

    • Aftabpasha
      New Member
      • Oct 2008
      • 32

      #3
      Thanks Jos,

      That means, arguments of a function call are not considered as Full expressions.

      One more dumb question.

      Code:
      int i=2;
      i = ++i;'
      In the above code, the expression i = ++i; according to the standards, should be an undefined behavior. But when I try to guess different possible outcomes of this expression, I can only think of 3 (as final value of i). How can it be undefined? Please put some light on the concept of Undefined Behavior in this case.

      Thank You.

      Regards,
      Aftab

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Aftabpasha
        Thanks Jos,

        That means, arguments of a function call are not considered as Full expressions.

        One more dumb question.

        Code:
        int i=2;
        i = ++i;'
        In the above code, the expression i = ++i; according to the standards, should be an undefined behavior. But when I try to guess different possible outcomes of this expression, I can only think of 3 (as final value of i). How can it be undefined? Please put some light on the concept of Undefined Behavior in this case.
        Strictly speaking (language lawyer's job) the behaviour should be undefined but because the possible outcomes of the evaluations are 3 and 3 (both the same) I'd call it defined behaviour. The Standard has gotten itself into trouble with this sequence point and undefined behavior stuff because canonical cases as you described all have a very simple (and defined!) outcome.

        kind regards,

        Jos

        Comment

        • Aftabpasha
          New Member
          • Oct 2008
          • 32

          #5
          Is there a sequence point at the end of a parametrized macro, just like a function?
          e.g.
          Code:
          #define SQR(x) ((x)*(x))
          So when we use SQR(10); is there a sequence point concept like function call?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Aftabpasha
            Is there a sequence point at the end of a parametrized macro, just like a function?
            e.g.
            Code:
            #define SQR(x) ((x)*(x))
            So when we use SQR(10); is there a sequence point concept like function call?
            Macros are just textually substituted so if you're looking for sequence points you have to look in the macro expanded text, e.g.

            Code:
            #define FOO(x) (x)+
            ...
            int x;
            x=FOO(3)1; // sequence point at the semicolon
            kind regards,

            Jos

            Comment

            • Aftabpasha
              New Member
              • Oct 2008
              • 32

              #7
              Please look at the code below.

              Code:
              int x =2;
              printf("%d, %d, %d", ++x, ++x, ++x);
              Now, by the rules, the printf statement has a potential of Undefined behavior. With a simple guess, the possible outputs are (3, 4, 5) or (5, 4, 3) (considering the two different orders of evaluation of arguments). Now, is there an such possibility of output like (4, 3, 5) or (3, 5, 4) or (5, 3, 4) or even (GARBAGE, GARBAGE, GARBAGE) (unpredictable) , and under which circumstances? Please put your views on this.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Aftabpasha
                Please look at the code below.

                Code:
                int x =2;
                printf("%d, %d, %d", ++x, ++x, ++x);
                Now, by the rules, the printf statement has a potential of Undefined behavior. With a simple guess, the possible outputs are (3, 4, 5) or (5, 4, 3) (considering the two different orders of evaluation of arguments). Now, is there an such possibility of output like (4, 3, 5) or (3, 5, 4) or (5, 3, 4) or even (GARBAGE, GARBAGE, GARBAGE) (unpredictable) , and under which circumstances? Please put your views on this.
                Undefined means undefined so anything can happen; the canonical symptom is that daemons will fly out of your nose. There is no need to discuss what actually will happen.

                kind regards,

                Jos

                Comment

                • Aftabpasha
                  New Member
                  • Oct 2008
                  • 32

                  #9
                  Thanks Josh for clearing my doubts.

                  Kind Regards,
                  Aftab

                  Comment

                  Working...