Ambiguous Expression

V

Vladimir

Colleagues,

Suppose I have (in simplified form, of course)

struct Vect2D {
double x, y;
Vect2D(double x_, double y_) : x(x_), y(y_) {}
};

which I have to construct with random numbers:

Vect2D v(rand(), rand());

But this is "Ambiguous Expression" - i.e. "...language does not
guarantee the order in which arguments to a function call are
evaluated."

I was beaten by such type of code indeed - release and debug builds
behave differently :(

Above code is so typical, do I have to force explicit order of argument
evaluation? It would not be so compact and nice.
 
V

Victor Bazarov

Vladimir said:
Colleagues,

Suppose I have (in simplified form, of course)

struct Vect2D {
double x, y;
Vect2D(double x_, double y_) : x(x_), y(y_) {}
};

which I have to construct with random numbers:

Vect2D v(rand(), rand());

But this is "Ambiguous Expression" - i.e. "...language does not
guarantee the order in which arguments to a function call are
evaluated."

I was beaten by such type of code indeed - release and debug builds
behave differently :(

Above code is so typical, do I have to force explicit order of argument
evaluation? It would not be so compact and nice.

Of course you do. You always have to account for side effects of function
calls. However, let me note here that one shouldn't really care about the
order when _random_ values are concerned, should one?

V
 
V

Vladimir

Victor said:
However, let me note here that one shouldn't really care about the
order when _random_ values are concerned, should one?

I thought so too :)
But the actual problem isn't the order itself. Modern compilers with
global opimizations and inlining will reach a situation like

call(++i, ++i);

which can result in both arguments being the same!
That's not really random to me :)

Of course you do. You always have to account for side effects of function
calls.

But C++ is quite powerful in allowing to use stuff on the fly which
really helps in large programs. What can be most compact, preferably
one-line solution to this problem then?
 
V

Victor Bazarov

Vladimir said:
I thought so too :)
But the actual problem isn't the order itself. Modern compilers with
global opimizations and inlining will reach a situation like

call(++i, ++i);

which can result in both arguments being the same!
That's not really random to me :)

The expression above has undefined behaviour because the object 'i' has
its _stored_value_ changed more than once between sequence points.
But C++ is quite powerful in allowing to use stuff on the fly which
really helps in large programs. What can be most compact, preferably
one-line solution to this problem then?

There isn't any. Use separately declared/defined/initialised objects:

int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);

Victor
 
I

Ioannis Vranos

Victor said:
There isn't any. Use separately declared/defined/initialised objects:

int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);


What about this?


int x;

Vect2D v((x=rand(), rand()), x);


:)
 
V

Vladimir

Victor said:
The expression above has undefined behaviour because the object 'i' has
its _stored_value_ changed more than once between sequence points.

Similar situation is with rand() - after inlining, internal value
(used to hold random state) is changed more than once - that's why
the problem occurs.

There isn't any. Use separately declared/defined/initialised objects:

int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);

I wish we could always remember to use this when needed.
What about encapsulating such functions with internal state
into some special classes which would prevent problems
(possibly by preventing inlining, etc.)?
This would make coding much more reliable - which is
essential in large serious projects.

P.S. These little things are really important, people.
They usually make the difference between 99% and 100%
bug-free software, so I think we shouldn't ignore them.
 
P

Pete Becker

Vladimir said:
Similar situation is with rand() - after inlining, internal value
(used to hold random state) is changed more than once - that's why
the problem occurs.

No, because there's a sequence point before each call to rand.
 
V

Victor Bazarov

Vladimir said:
Similar situation is with rand() - after inlining, internal value
(used to hold random state) is changed more than once - that's why
the problem occurs.

Similar, but no undefined behaviour, only unspecified order of calls.
Every function call is surrounded by sequence points, so even with
inlining there would be at least four of them between the program
decided to call the first 'rand' and calling the 'call' function. So,
the change to some stored value (the side effect of 'rand') does not
happen more than once between sequence points.
I wish we could always remember to use this when needed.

And I wish I were young, slim, and healthy.
What about encapsulating such functions with internal state
into some special classes which would prevent problems
(possibly by preventing inlining, etc.)?

You can try limiting the members of your programming team to using
some kind of class for that, or a macro, or whatever would resolve
this issue, but the language does not provide a mechanism (yet) to
catch all instances of unspecified behaviour. Of course we can always
hope for better tools at our disposal...
This would make coding much more reliable - which is
essential in large serious projects.

I believe you could use some kind of "PC-lint"-like code checker that
might catch that.
P.S. These little things are really important, people.
They usually make the difference between 99% and 100%
bug-free software, so I think we shouldn't ignore them.

Of course we shouldn't. And _we_ won't. It's the programmers who don't
read comp.lang.c++ we should be worrying about :)

V
 
V

Vladimir

Victor said:
So, the change to some stored value (the side effect of 'rand')
does not happen more than once between sequence points.

Here's smallest code reproducing the problem, where func() represents
typical rand() implementation in very simplified form:

inline int func()
{
static int state = 0;
return ++state;
}

int main()
{
std::cout << func() << func() << std::endl;
return 0;
}

Debug build produces "21" which is ok, but Release build
outputs "22" which ruins expected sequence-generating behavior.
I tested it with vc++ 6.0 and I'm interested what other compilers
would offer (note: global optimizations were heavily used).
 
V

Victor Bazarov

Vladimir said:
Here's smallest code reproducing the problem, where func() represents
typical rand() implementation in very simplified form:

inline int func()
{
static int state = 0;
return ++state;
}

To make it compile on all compilers, add:

#include <iostream> // for 'std::cout'
#include said:
int main()
{
std::cout << func() << func() << std::endl;
return 0;
}

Debug build produces "21" which is ok, but Release build
outputs "22" which ruins expected sequence-generating behavior.
I tested it with vc++ 6.0 and I'm interested what other compilers
would offer (note: global optimizations were heavily used).

VC++ v7.1 produces "21" in debug mode and "22" in release mode (no
surprises there).

VC++ v8.0 Beta produces "21" in both debug and release modes.

Which is not to say that any of them are "correct", only that they
differ, as they may.

Victor
 
P

Pete Becker

=> VC++ v7.1 produces "21" in debug mode and "22" in release mode (no
surprises there).

VC++ v8.0 Beta produces "21" in both debug and release modes.

Which is not to say that any of them are "correct", only that they
differ, as they may.

That is, "21" or "12" is okay, but "22" is definitely wrong, because it
violates the rules about sequence points.
 
V

Victor Bazarov

Pete said:
=> VC++ v7.1 produces "21" in debug mode and "22" in release mode (no


That is, "21" or "12" is okay, but "22" is definitely wrong, because it
violates the rules about sequence points.

Ah... Good point (no pun intended). So, optimization should not prevent
any side effects from taking place, yes? My guess is that circumventing
side effects is only allowed in particular cases and they are described in
the Standard explicitly.

V
 
P

Pete Becker

Victor said:
Ah... Good point (no pun intended). So, optimization should not prevent
any side effects from taking place, yes? My guess is that circumventing
side effects is only allowed in particular cases and they are described in
the Standard explicitly.

They're not described explicitly, but the "as if" rule (1.5/1) is the
thing to look to. The standard specifies the observable behavior of
well-formed programs. The behavior of this program depends on
unspecified behavior, but that doesn't make it ill-formed, so the
compiler must produce a program with the observable behavior specified
by the standard. It can't blow away the sequence point after the first
call (in the generated code, not the fist in the source code) to func.
 
V

Vladimir

So,

vc++ v6.0 debug "21", release "22"
vc++ v7.1 debug "21", release "22"
vc++ v8.0 Beta "21" in both debug and release modes.

Something tells me v8.0 *Final* will catch up with previous versions :)
ok, it's OT - I'm hiding right now.
 
V

Vladimir

Pete said:
by the standard. It can't blow away the sequence point after the first
call (in the generated code, not the fist in the source code) to
func.


IMHO there is no sequence point between arg1 and arg2 evaulations in a
call(arg1, arg2);

is it?
 
V

Victor Bazarov

Vladimir said:
func.


IMHO there is no sequence point between arg1 and arg2 evaulations in a
call(arg1, arg2);

is it?

It depends on what 'arg1' and 'arg2' are. If they are function calls,
there is _always_ a sequence point between them (before the second call,
which is not necessarily to evaluate 'arg2'). That's what Pete said.

And it has nothing to do with opinions. It's specified by the Standard.

V
 
A

Attila Feher

Ron said:
That's worse. The second argument to the
v initializer may be evaluated before the first.

That said, it could be fun to make a contest (like the obfuscated C one) for
C++. Where the code looks like doing one thing, but in fact it does another
(or nothing specified). I know that the above is "only C", but in C++ we
could put some spice on it by having converting constructors and implicit
conversion operators, broken copy constructors, reference members etc. ;-)
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top