Topic: Sequence Point Question


Author: jdavison@shell.inch.com (John Michael Davison)
Date: 1999/04/22
Raw View
        Clause 5, subclause 4 p. 63, of ISO/IEC 14882:1998(E) talks about the
behavior of such expressions as

          i = v[i++]
          i = ++i + 1

as being unspecified; Clause 3, subclause 3, p. 39, lines 5-7 of American
National Standard X3.159-1989 say the same thing about C.

        Does this mean that the behavior of the following program is
unspecified?  (I assume, from a quick look at Appendix B of American National
Standard X3.159-1989, that it indeed would be unspecified because the return
statement expression in each of "foo" and "bar" constitutes a single "sequence
point.")

//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

int foo(int &lhs, int &rhs)
{
    return ++lhs + ++rhs;
}

int bar(int *lhs, int *rhs)
{
    return ++*lhs + ++*rhs;
}

int
main(void)
{
    int a = 5;

    printf("foo(a, a) = %d\n", foo(a, a));

    a = 5;

    printf("bar(&a, &a) = %d\n", bar(&a, &a));

    return EXIT_SUCCESS;
}

//-----------------------------------------------------------------------------

        When compiled with the archaic Centerline 2.2 C++ complier (on SunOS
4.1.3_U1), the program issues the following:

          foo(a, a) = 13
          bar(&a, &a) = 13

        When compiled with the somewhat less archaic, but still pre-ISO, Sun
SPARCworks 4.2 C++ compiler (on SunOS 5.5.1), the following is printed:

          foo(a, a) = 14
          bar(&a, &a) = 14


Thanks,

--
John Davison
jdavison@inch.com
--
John Davison
jdavison@inch.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]