Sequence points and undefined behaviour 
Author Message
 Sequence points and undefined behaviour

Is it possible to have more than one sequence
point in an expression? And if so is it possible
to have an expression which modifies the value
of a variable more tham once and is not invoking
UB?

--
Thomas.

Approaching singularity.



Mon, 31 May 2004 04:58:27 GMT  
 Sequence points and undefined behaviour

Quote:

> Is it possible to have more than one sequence
> point in an expression?

Yes.  For instance, you could use the &&, ||, or , operators, all
of which contain sequence points.

Quote:
> And if so is it possible
> to have an expression which modifies the value
> of a variable more tham once and is not invoking
> UB?

Yes; e.g.,
        x = foo(), x += bar;
which is a silly way of writing
        x = foo() + bar;


Mon, 31 May 2004 05:12:10 GMT  
 Sequence points and undefined behaviour

Quote:

>Is it possible to have more than one sequence
>point in an expression?

Absolutely; the operators ||  ?:  && and ,   introduce a sequence point.

There can be independent sequence point in subexpressions; these sequence
points are not ordered with respect to each other, e.g:

        (a, b) + (c, d)

Quote:
>And if so is it possible
>to have an expression which modifies the value
>of a variable more tham once and is not invoking
>UB?

Yes, for example

        i++ && i++ || i++, i++



Mon, 31 May 2004 05:32:23 GMT  
 Sequence points and undefined behaviour

Quote:
> Is it possible to have more than one sequence
> point in an expression?

At least 125 or so are possible (maybe a lot more).  It's the maximum
allowed line length that puts a cap on it.

Quote:
> And if so is it possible
> to have an expression which modifies the value
> of a variable more tham once and is not invoking
> UB?

Sure.  Just not between sequence points.
int             main(void)
{
    int             i,
                    j = 1;
i=++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++
j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++
j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++
j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j;
    printf("%d\n", ++j);
Quote:
}

--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm


Mon, 31 May 2004 05:51:05 GMT  
 Sequence points and undefined behaviour

Quote:



> > Is it possible to have more than one sequence
> > point in an expression?

> At least 125 or so are possible (maybe a lot more).  It's the maximum
> allowed line length that puts a cap on it.

How does the maximum allowed line length limit the length of an
expression?  Last I heard, expressions were allowed to extend
across as many lines as necessary.  I don't see an environmental
limit in the standard limiting the number of lines or characters
in an expression, just one for the number of parentheses (C99
5.4.2.1):

     - 63 nesting levels of parenthesized expressions within a
       full expression



Mon, 31 May 2004 06:24:24 GMT  
 Sequence points and undefined behaviour

Quote:



> > > Is it possible to have more than one sequence
> > > point in an expression?

> > At least 125 or so are possible (maybe a lot more).  It's the maximum
> > allowed line length that puts a cap on it.

> How does the maximum allowed line length limit the length of an
> expression?  Last I heard, expressions were allowed to extend
> across as many lines as necessary.  I don't see an environmental
> limit in the standard limiting the number of lines or characters
> in an expression, just one for the number of parentheses (C99
> 5.4.2.1):

>      - 63 nesting levels of parenthesized expressions within a
>        full expression

5.2.4.1 Translation limits
...
- 4095 characters in a logical source line
...

Question:
What is a logical source line?  I have been told that this is not the same
as the physical source line.  I had problems a long time ago with large
character constants, and was told that:
char *foo =
"long bunch of characters............"
"Another long bunch of characters...."
...
"Final bunch of characters...........";

Was a violation of two constraints -- character string literal length and
line length.


"What exactly is a 'logical source line' and how does it differ from a
physical source line?"

Is my understanding that:

int j = 1;
i=
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
++j,++j,++j;

violates the old limit of 510 {IIRC} characters not correct, as long as no
single line spans 512 characters?

--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm



Mon, 31 May 2004 08:01:56 GMT  
 Sequence points and undefined behaviour

Quote:






> > > > Is it possible to have more than one sequence
> > > > point in an expression?

> > > At least 125 or so are possible (maybe a lot more).  It's the maximum
> > > allowed line length that puts a cap on it.

> > How does the maximum allowed line length limit the length of an
> > expression? [...]
> 5.2.4.1 Translation limits
> ...
> - 4095 characters in a logical source line
> ...

> Question:
> What is a logical source line?

This is implicitly defined by C99 5.1.1.2:

        2.  Each instance of a backslash character (\)
            immediately followed by a new-line character is
            deleted, splicing physical source lines to form
            logical source lines.

So a logical source line is a physical source line after source
character set mapping and trigraph replacement, or a set of these
lines after backslash splicing.

Quote:
> I have been told that this is not the same
> as the physical source line.  I had problems a long time ago with large
> character constants, and was told that:
> char *foo =
> "long bunch of characters............"
> "Another long bunch of characters...."
> ...
> "Final bunch of characters...........";

> Was a violation of two constraints -- character string literal length and
> line length.

I can believe the former but the latter makes less sense.  I
guess I could see it if translation phase 6 produces a logical
source line, but that's not what the standard says AFAICT.

Quote:
> Is my understanding that:

> int j = 1;
> i=
> ++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,++j,
[...]

> violates the old limit of 510 {IIRC} characters not correct, as long as no
> single line spans 512 characters?

No translation phase is stated to cause expressions to be
gathered into logical source lines, but phase 6 *might* be
considered to do so for adjacent string literals.
--
"C has its problems, but a language designed from scratch would have some too,
 and we know C's problems."
--Bjarne Stroustrup


Mon, 31 May 2004 08:32:37 GMT  
 Sequence points and undefined behaviour
...

Quote:
> What is a logical source line?  I have been told that this is not the same
> as the physical source line.

Physical source lines end with newline characters; logical source lines
end with unescaped newline characters. If you put a '\' at the end of a
physical source line, it escapes the newline, causing the physical
source line to get merged with the following physical source line, to
form a single logical source line. Example:

        int \
        i
        ; i=1;

That constitutes three physical source lines, but only two seperate
logical source lines.



Mon, 31 May 2004 08:45:40 GMT  
 Sequence points and undefined behaviour

Quote:

>Question:
>What is a logical source line?

As you know, of course, one can indicate line continuation in C
using the characters backslash-newline, which are treated in the
very first translation phase. When you use that, you are splicing
two or more physical lines into a single logical line. In the
absence of that splicing, there is a one-to-one correspondence between
physical and logical lines.

Quote:
> I have been told that this is not the same
>as the physical source line.  I had problems a long time ago with large
>character constants, and was told that:
>char *foo =
>"long bunch of characters............"
>"Another long bunch of characters...."
>...
>"Final bunch of characters...........";

>Was a violation of two constraints -- character string literal length and
>line length.

No, only string literal length; there are multiple physical lines here.
The catenation of string literals is done in a much later translation
phase, when lines no longer matter.


Mon, 31 May 2004 08:46:18 GMT  
 Sequence points and undefined behaviour


Quote:
> Is it possible to have more than one sequence
> point in an expression? And if so is it possible
> to have an expression which modifies the value
> of a variable more tham once and is not invoking
> UB?

Yes. For N sequence points, have a full expression with
N-1 comma operators. (There are other operators that
will do, alone or in combinaton, but this is the obvious
one.)

Hence, yes.

--
Obvious To *Me* Hedgehog
C FAQ's at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Mon, 31 May 2004 14:55:08 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. why is undefined behaviour undefined ?

2. Does pointer comparison cause undefined behaviour?

3. Most bizarre undefined behaviour

4. Undefined behaviour?

5. Undefined behaviour

6. Ternary bug or undefined behaviour

7. Undefined behaviour?

8. Undefined behaviour - Was: .9 REPEATING IS IT ONE?

9. Undefined behaviour - or not???

10. Undefined behaviour story

11. malloc,calloc undefined behaviour ??

12. Is this undefined behaviour?

 

 
Powered by phpBB® Forum Software