473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

order of subexpression evaluation

Hello,

evaluating

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

i would expect

calculate foo(bar(100.0)
calculate foo(bar(10.0)
divide
(or maybe foo(bar(10));foo(bar(100));reversed divide;)

but i have a compiler which splits the foo/bar calls to:
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;
}

(the result of 100/10 is 1.)

is this allowed order of subexpr.evaluation or a compiler bug?

Note : some compiler behave different whether foo is a function or a macro:

#define foo(d) (*(d))

--
mit freundlichen Grüßen/best regards
mario semo
Jun 27 '06 #1
2 1999
* mario semo:
evaluating

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

i would expect

calculate foo(bar(100.0)
calculate foo(bar(10.0)
divide
(or maybe foo(bar(10));foo(bar(100));reversed divide;)


The compiler is free to evaluate the subexpressions in any order,
subject to the constraint that a function's arguments must necessarily
all be evaluated before actually calling that function.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 27 '06 #2
Flo
Hello

If you google for c++ and sequence points you find information on how
the c++ compiler has to evaluate the syntax tree ("sub-expression
tree"): What he is free to do and where it is restricted by some rules.

In short and not very exact: Sub-expressions can be evaluated in any
order. However at sequence points, everything must be done. I.e. all
side effects must be completed. Sequence points are for example the
operators ; && || ?: , function call

Your compiler behaves correctly. The only thing he has to do is
- evaluate 100.0 before calling bar with it as argument
- evaluate 10.0 before calling bar with it as argument
- evaluate bar(100.0) before calling foo with it as argument
- evaluate bar(10.0) before calling foo with it as argument

This 4 rules leave the compiler quite a lot of freedom in which order
it evaluates all the sub expressions.

A side which goes into the details:
http://www.open-std.org/jtc1/sc22/wg.../docs/n926.htm

Greetings

Flo

mario semo wrote:
Hello,

evaluating

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

i would expect

calculate foo(bar(100.0)
calculate foo(bar(10.0)
divide
(or maybe foo(bar(10));foo(bar(100));reversed divide;)

but i have a compiler which splits the foo/bar calls to:
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;
}

(the result of 100/10 is 1.)

is this allowed order of subexpr.evaluation or a compiler bug?

Note : some compiler behave different whether foo is a function or a macro:

#define foo(d) (*(d))

--
mit freundlichen Grüßen/best regards
mario semo


Jun 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
7
by: publictom | last post by:
Is the following statement true? "It is required that the operands to an operator be fully evaluated before the operator itself is evaluated." The only way I can think of that it would be...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
15
by: Jens.Toerring | last post by:
Hi, I have a possibly rather stupid question about the order of evaluation in a statement like this: result = foo( x ) - bar( y ); How can I make 100% sure that foo(x) is evaluated before...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.