Discussion:
VACPP 3.5.x Win : wrong code?
(too old to reply)
Mario Semo
2006-06-26 08:35:10 UTC
Permalink
Just in case someone with better knowledge of C/C++ rules read this and has
some comments:

evaluating

(foo(bar(100.0))) / foo(bar(10.0)))

i would expect

calculate foo(bar(100.0)
calculate foo(bar(10.0)
divide

but VACPP 3.5 calculates
reg1=bar(100.0)
reg2=bar(10.0)
foo(reg1)
foo(reg2)
divide

this yields incorrect results in the following sample :

double * bar(double d)
{
static double _sd;
_sd = d;
return &_sd;
}

double foo(double *d)
{
return *d;
}

(VACPP's result of 100/10 is 1.)
--
mit freundlichen Grüßen/best regards
mario semo
David
2006-06-26 20:26:35 UTC
Permalink
Hello Mario,
Post by Mario Semo
Just in case someone with better knowledge of C/C++ rules read this and has
evaluating
(foo(bar(100.0))) / foo(bar(10.0)))
i would expect
calculate foo(bar(100.0)
calculate foo(bar(10.0)
divide
but VACPP 3.5 calculates
reg1=bar(100.0)
reg2=bar(10.0)
foo(reg1)
foo(reg2)
divide
double * bar(double d)
{
static double _sd;
_sd = d;
return &_sd;
}
double foo(double *d)
{
return *d;
}
(VACPP's result of 100/10 is 1.)
Nearly all compilers can be coaxed into performing
this sequence of operations. It is also totally
legal in terms of the language.

foo() and bar() are operations similar to ++ and --
in that when they are used multiple times in the
same statement, they can yield incorrect results
due to the poor assumptions of the coder.

In your example, several operations are designed
so that they work to show that the order of
certain operations is not known for optimized
code. Further you use code (bar()) that any
developer should recognize as non-reentrant.
As you've discovered, it can be rather easy to
call a function twice without realizing it.

What makes you think that VAC++ is responsible
for the unexpected result and not the developer?

The comp.lang.c and comp.lang.c++ groups deal
with the standards for the C and C++ languages
and persons there may be able to quote the
various rules that state what your code is
supposed to do and what is left to
interpretation by the compiler. The order of
operations in a single statement having equal
precedence can be performed in any order the
compiler or computer chooses.

David

Loading...