Topic: mutiple ++ operators in same expression
Author: asbinn@rstcorp.com
Date: 1998/05/04 Raw View
In article <6iff8g$i8q@engnews1.Eng.Sun.COM>#1/1,
clamage@Eng.Sun.COM (Steve Clamage) wrote:
>
> The sample code looked like this:
>
> >output : command (or parial cmd) that generated the output
> >---------:----------------------------------------------------------
> >(3,3,3) : Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]))
>
> The code attempts to modify a variable more than once between
> sequence points, and so the behavior is undefined. That has
> always been the case in C, and is still the case in C++.
I thought that this example yields unspecified behavior, not undefined
behavior. The reason is the unspecified order of function argument
evaluation. Isn't there a sequence point after each call to atof()
(assuming that atof() is a function call and not a macro) [intro.execution
p17]? If so, then 'i' isn't modified more than once between sequence
points.
The arguments to Vector3f constructor will be evaluated in an un-specified
order, but there is a sequence point after each one. If 'i' had the value
of 2, then there should be six possible outcomes:
(3,4,5)
(3,5,4)
(4,3,5)
(4,5,3)
(5,3,4)
(5,4,3)
Whether or not the behavior is classified as undefined or unspecified
is probably of little consequence to most programmers, as relying on either
one will lead to unreliable programs.
Aaron
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
[ 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 ]
Author: buesing@demaco1.demaco.com
Date: 1998/05/01 Raw View
Can anyone tell me if the following are defined by the ANSI C++ standard,
and if so. What the correct results should be?
I have included the test code below, and this is the results with
various compiliers on various OSes.
They all produce different output with the same test case. I realize
the use of ++i in the same expression may be undefine, so implementors
are able to code it as desired. However, I would like clarification
on this (if possible).
Thanks,
Neil Buesing
buesing@demaco1.demaco.com
SGI : IRIX6.3 : MIPSpro Compilers: Version 7.20 (n32)
%CC -n32 test.C
%./a.out
output : command (or parial cmd) that generated the output
---------:----------------------------------------------------------
(3,3,3) : Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]))
(3,3,3) : Vector3f b(f[++i], f[++i], f[++i])
3 3 3 : cout << ++i << << ++i << << ++i
1 2 3 : cout << a1 << << a2 << << a3
3 3 3 : cout << f[++i] << << f[++i] << << f[++i]
2 2 2 : cout << i++ << << i++ << << i++
1 2 3 : cout << b1 << << b2 << << b3
SGI : IRIX6.3 : MIPSpro Compilers: Version 7.20 (o32)
%CC -32 test.C
%./a.out
output : command (or parial cmd) that generated the output
---------:----------------------------------------------------------
(1,2,3) : Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]))
(3,3,3) : Vector3f b(f[++i], f[++i], f[++i])
1 2 3 : cout << ++i << << ++i << << ++i
1 2 3 : cout << a1 << << a2 << << a3
1 2 3 : cout << f[++i] << << f[++i] << << f[++i]
0 1 2 : cout << i++ << << i++ << << i++
1 2 3 : cout << b1 << << b2 << << b3
SGI : IRIX6.2 : Mongoose Compilers: Version 7.10 (n32)
%CC -n32 test.C
%./a.out 1 2 3 4
output : command (or parial cmd) that generated the output
---------:----------------------------------------------------------
(1,2,3) : Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]))
(3,3,3) : Vector3f b(f[++i], f[++i], f[++i])
1 2 3 : cout << ++i << << ++i << << ++i
1 2 3 : cout << a1 << << a2 << << a3
1 2 3 : cout << f[++i] << << f[++i] << << f[++i]
0 1 2 : cout << i++ << << i++ << << i++
1 2 3 : cout << b1 << << b2 << << b3
SUN : Solaris2.5.1 : CC: WorkShop Compilers 4.2 18 Sep 1997 C++ 4.2
%CC test.C
%./a.out
output : command (or parial cmd) that generated the output
---------:----------------------------------------------------------
(3,3,3) : Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]))
(3,3,3) : Vector3f b(f[++i], f[++i], f[++i])
1 2 3 : cout << ++i << << ++i << << ++i
1 2 3 : cout << a1 << << a2 << << a3
3 3 3 : cout << f[++i] << << f[++i] << << f[++i]
0 1 2 : cout << i++ << << i++ << << i++
1 2 3 : cout << b1 << << b2 << << b3
/////////////////
//test.C : BEGIN
//
#include <iostream.h>
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////////////
class Vector3f {
private:
float _vector[3];
public:
Vector3f(float, float, float);
friend ostream &operator<<(ostream &os, const Vector3f &v);
inline const float& operator[](int i) const;
inline float& operator[](int i);
};
///////////////////////////////////////////////////////////////////////////////
Vector3f::Vector3f(float x, float y, float z) {
_vector[0] = x;
_vector[1] = y;
_vector[2] = z;
}
///////////////////////////////////////////////////////////////////////////////
inline const float& Vector3f::operator[](int i) const {
return _vector[i];
}
///////////////////////////////////////////////////////////////////////////////
inline float& Vector3f::operator[](int i) {
return _vector[i];
}
///////////////////////////////////////////////////////////////////////////////
ostream &operator<<(ostream &os, const Vector3f &v) {
os << "(" << v[0] << "," << v[1] << "," << v[2] << ")";
return os;
}
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
int i=0;
float f[4] = {0, 1, 2, 3};
const char *str[4] = { "0", "1", "2", "3" };
//
cout << "output : command (or parial cmd) that generated the output" <<
endl;
cout <<
"---------:----------------------------------------------------------"
<< endl;
//Case #1
i=0;
Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]));
cout << a
<< " : Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]))"
<< endl;
//Case #2
i=0;
Vector3f b(f[++i], f[++i], f[++i]);
cout << b;
cout << " : Vector3f b(f[++i], f[++i], f[++i])"
<< endl;
//Case #3
i=0;
cout << ++i << " " << ++i << " " << ++i
<< " : cout << ++i << " " << ++i << " " << ++i"
<< endl;
//Case #4
i=0;
int a1 = ++i;
int a2 = ++i;
int a3 = ++i;
cout << a1 << " " << a2 << " " << a3
<< " : cout << a1 << " " << a2 << " " << a3"
<< endl;
//Case #5
i=0;
cout << f[++i] << " " << f[++i] << " " << f[++i]
<< " : cout << f[++i] << " " << f[++i] << " " << f[++i]"
<< endl;
//Case #6
i=0;
cout << i++ << " " << i++ << " " << i++
<< " : cout << i++ << " " << i++ << " " << i++"
<< endl;
//Case #7
i=0;
int b1, b2, b3;
b1=++i,b2=++i,b3=++i;
cout << b1 << " " << b2 << " " << b3;
cout << " : cout << b1 << " " << b2 << " " << b3"
<< endl;
}
//
// test.C : END
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
[ 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 ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/05/03 Raw View
buesing@demaco1.demaco.com writes:
>Can anyone tell me if the following are defined by the ANSI C++ standard,
>and if so. What the correct results should be?
>I have included the test code below, and this is the results with
>various compiliers on various OSes.
>They all produce different output with the same test case. I realize
>the use of ++i in the same expression may be undefine, so implementors
>are able to code it as desired. However, I would like clarification
>on this (if possible).
The sample code looked like this:
>output : command (or parial cmd) that generated the output
>---------:----------------------------------------------------------
>(3,3,3) : Vector3f a(atof(str[++i]), atof(str[++i]), atof(str[++i]))
>(3,3,3) : Vector3f b(f[++i], f[++i], f[++i])
>3 3 3 : cout << ++i << << ++i << << ++i
>3 3 3 : cout << f[++i] << << f[++i] << << f[++i]
>2 2 2 : cout << i++ << << i++ << << i++
The code attempts to modify a variable more than once between
sequence points, and so the behavior is undefined. That has
always been the case in C, and is still the case in C++.
See the C++ draft standard, chapter 5, paragraph 4, which even
contains examples like yours.
---
Steve Clamage, stephen.clamage@sun.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 ]
Author: "sweet boy" <anson@mail.com>
Date: 1998/05/03 Raw View
For the statement:
cout<<++i<<' '<<++i<<' '<<++i<<endl;
The output should be 4 5 6, rather than 3 3 3 if i was initialized as 3.
--
sweet boy
e-mail: anson@mail.com
ICQ UIN: 2056592
homepage: http://www.geocities.com/~foreverland
[ 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 ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/05/04 Raw View
"sweet boy" <anson@mail.com> writes:
>For the statement:
>cout<<++i<<' '<<++i<<' '<<++i<<endl;
>The output should be 4 5 6, rather than 3 3 3 if i was initialized as 3.
No, the results are undefined, as I explained in another article
with the same subject. You can't expect anything in particular
as program output.
--
Steve Clamage, stephen.clamage@sun.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 ]