473,847 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

controlling expressions evaluation

Hi group,
here I come with a question which is quite simple per se, but for which
I can't find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?

Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?

The reason for my question is that in my code I have to fill a structure
(say "my_table") with a function (say "fill_table()") , and fill_table
should be called only if my_table is NULL (i.e. wasn't filled before).

if this always going to be correct?

if(!my_table && fill_table())

Thank you.
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 9 '07 #1
11 1675
On Jul 9, 5:04 pm, Pietro Cerutti <g...@gahr.chwr ote:
Hi group,
here I come with a question which is quite simple per se, but for which
I can't find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?
C standards gurantee left to right evlauation in this respect.
The C standards is clear about this.

Here I quote 6.5.13 Logical AND operator:

Syntax
1 logical-AND-expression:
inclusive-OR-expression
logical-AND-expression && inclusive-OR-expression
Constraints
2 Each of the operands shall have scalar type.
Semantics
3 The && operator shall yield 1 if both of its operands compare
unequal to 0; otherwise, it
yields 0. The result has type int.
4 Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation;
there is a sequence point after the evaluation of the first operand.
If the first operand
compares equal to 0, the second operand is not evaluated

I hope this should answer your doubt completely.
>
Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?
As per the standards, 'y' won't be evaluated first. But by "as if"
rule, I think a compiler might do that, if it doesn't affect the
behavior of the code. But ultimately the result produced by the code
should be such that, there is left to right evaluation.

So you could very well think that 'x' is evaluated before 'y' without
bothering, if it is internally done that way or not.
>
The reason for my question is that in my code I have to fill a structure
(say "my_table") with a function (say "fill_table()") , and fill_table
should be called only if my_table is NULL (i.e. wasn't filled before).

if this always going to be correct?

if(!my_table && fill_table())

Thank you.
Jul 9 '07 #2
Pietro Cerutti said:

<snip>
I'm only a bit worried about your sentence "[...] in some situations
you might find that the evaluation of y begins before the evaluation
of x".
Don't be. The "as if" rule applies.
What does it mean, practically?
Practically, it means you don't need to be a bit worried. :-)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 9 '07 #3
Pietro Cerutti wrote:
Eric Sosman wrote:
>Pietro Cerutti wrote:
>>Hi group,
here I come with a question which is quite simple per se, but for which
I can't find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?
Yes.
>>Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?
Yes. If you study the generated code you might doubt
this "yes," because in some situations you might find that
the evaluation of y begins before the evaluation of x is
finished (imagine x and y not as single variables, but as
more complicated expressions). But don't worry: the outcome
must be "as if" y's evaluation is strictly later than x's,
or doesn't happen at all if x is zero. No side-effects or
errors that could occur in the evaluation of y can happen
until after x is finished and found to be non-zero; if x
turns out to be zero then no side-effects or errors from
evaluating y can occur at all.

Thank you for your answer (and also thank you CryptiqueGuy for yours,
which is basically the same).

I'm only a bit worried about your sentence "[...] in some situations you
might find that the evaluation of y begins before the evaluation of x".
What does it mean, practically?
Essentially, it means "Don't let the generated code
confuse you." Imagine a y that is a multi-term expression,
and suppose that some parts of y will be used no matter
how the test comes out:

if (x && r + s t)
printf ("Boo!\n");
z = r + s + t;

Here, y is the entire expression `r + s t', which we have
said will only be evaluated if x is non-zero. But the compiler
might decide to evaluate the sub-expression `r + s' early, since
it will produce no side-effects (let's assume) and the sum will
be needed anyhow. The compiler might do something like

tmp = r + s;
if (x && tmp t)
printf ("Boo!\n");
z = tmp + t;

.... and this would be perfectly all right. The compiler could
*not* do this if evaluating `r + s' could produce a side-effect
(overflow generating a signal, for instance), because the value
of x should govern whether the signal occurs before or after the
output, and in the rewritten code it would always occur first.
But if the compiler can figure out that `r + s' has no side-
effects to worry about, it can evaluate that component of y
before it finishes evaluating x.

Practical effects: From the C programmer's perspective,
none, because the program operates "as if" x==0 completely
suppressed the evaluation of y. Someone studying the compiled
code might be confused by seeing some parts of y evaluated
early, but that's just a distraction. The only reason I brought
it up was in response to your phrase "for whatever purpose."

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 9 '07 #4
Eric Sosman wrote:
Pietro Cerutti wrote:
>Eric Sosman wrote:
>>Pietro Cerutti wrote:
Hi group,
here I come with a question which is quite simple per se, but for which
I can't find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?
Yes.

Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?
Yes. If you study the generated code you might doubt
this "yes," because in some situations you might find that
the evaluation of y begins before the evaluation of x is
finished (imagine x and y not as single variables, but as
more complicated expressions). But don't worry: the outcome
must be "as if" y's evaluation is strictly later than x's,
or doesn't happen at all if x is zero. No side-effects or
errors that could occur in the evaluation of y can happen
until after x is finished and found to be non-zero; if x
turns out to be zero then no side-effects or errors from
evaluating y can occur at all.

Thank you for your answer (and also thank you CryptiqueGuy for yours,
which is basically the same).

I'm only a bit worried about your sentence "[...] in some situations you
might find that the evaluation of y begins before the evaluation of x".
What does it mean, practically?

Essentially, it means "Don't let the generated code
confuse you." Imagine a y that is a multi-term expression,
and suppose that some parts of y will be used no matter
how the test comes out:

if (x && r + s t)
printf ("Boo!\n");
z = r + s + t;

Here, y is the entire expression `r + s t', which we have
said will only be evaluated if x is non-zero. But the compiler
might decide to evaluate the sub-expression `r + s' early, since
it will produce no side-effects (let's assume) and the sum will
be needed anyhow. The compiler might do something like

tmp = r + s;
if (x && tmp t)
printf ("Boo!\n");
z = tmp + t;

... and this would be perfectly all right. The compiler could
*not* do this if evaluating `r + s' could produce a side-effect
(overflow generating a signal, for instance), because the value
of x should govern whether the signal occurs before or after the
output, and in the rewritten code it would always occur first.
But if the compiler can figure out that `r + s' has no side-
effects to worry about, it can evaluate that component of y
before it finishes evaluating x.

Practical effects: From the C programmer's perspective,
none, because the program operates "as if" x==0 completely
suppressed the evaluation of y. Someone studying the compiled
code might be confused by seeing some parts of y evaluated
early, but that's just a distraction. The only reason I brought
it up was in response to your phrase "for whatever purpose."
Thank you, your example was clear!

One more question, if I can...

What if "y" is a function modifying "x"? Can the compiler figure our
that evaluating "y" has effects on "x"? In this case, is the function
not entered at all, or stopped before modifying "x"?

Thank you again.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 9 '07 #5
Pietro Cerutti <ga**@gahr.chwr ites:
Eric Sosman wrote:
>Pietro Cerutti wrote:
>>Eric Sosman wrote:
Pietro Cerutti wrote:
Hi group,
here I come with a question which is quite simple per se, but for which
I can't find an answer.
>
Does the C standard guarantee that inside an expression such as
>
(x && y)
>
"y" is not evaluated if "x" evaluates to 0?
Yes.

Are we guaranteed that a conforming compiler cannot, for whatever
purpose, evaluate "y" first?
Yes. If you study the generated code you might doubt
this "yes," because in some situations you might find that
the evaluation of y begins before the evaluation of x is
finished (imagine x and y not as single variables, but as
more complicated expressions). But don't worry: the outcome
must be "as if" y's evaluation is strictly later than x's,
or doesn't happen at all if x is zero. No side-effects or
errors that could occur in the evaluation of y can happen
until after x is finished and found to be non-zero; if x
turns out to be zero then no side-effects or errors from
evaluating y can occur at all.

Thank you for your answer (and also thank you CryptiqueGuy for yours,
which is basically the same).

I'm only a bit worried about your sentence "[...] in some situations you
might find that the evaluation of y begins before the evaluation of x".
What does it mean, practically?

Essentially, it means "Don't let the generated code
confuse you." Imagine a y that is a multi-term expression,
and suppose that some parts of y will be used no matter
how the test comes out:

if (x && r + s t)
printf ("Boo!\n");
z = r + s + t;

Here, y is the entire expression `r + s t', which we have
said will only be evaluated if x is non-zero. But the compiler
might decide to evaluate the sub-expression `r + s' early, since
it will produce no side-effects (let's assume) and the sum will
be needed anyhow. The compiler might do something like

tmp = r + s;
if (x && tmp t)
printf ("Boo!\n");
z = tmp + t;

... and this would be perfectly all right. The compiler could
*not* do this if evaluating `r + s' could produce a side-effect
(overflow generating a signal, for instance), because the value
of x should govern whether the signal occurs before or after the
output, and in the rewritten code it would always occur first.
But if the compiler can figure out that `r + s' has no side-
effects to worry about, it can evaluate that component of y
before it finishes evaluating x.

Practical effects: From the C programmer's perspective,
none, because the program operates "as if" x==0 completely
suppressed the evaluation of y. Someone studying the compiled
code might be confused by seeing some parts of y evaluated
early, but that's just a distraction. The only reason I brought
it up was in response to your phrase "for whatever purpose."

Thank you, your example was clear!

One more question, if I can...

What if "y" is a function modifying "x"? Can the compiler figure our
that evaluating "y" has effects on "x"? In this case, is the function
not entered at all, or stopped before modifying "x"?
How or why would a compiler allow that function to be called and then
"stop it before modifying x"?

y is not evaluated if x is true.
>
Thank you again.
--
Jul 9 '07 #6
Pietro Cerutti wrote:
Eric Sosman wrote:
Pietro Cerutti wrote:
Eric Sosman wrote:
Pietro Cerutti wrote:
Hi group,
here I come with a question which is quite simple per se, but for which
I can't find an answer.

Does the C standard guarantee that inside an expression such as

(x && y)

"y" is not evaluated if "x" evaluates to 0?
Yes.
<snip>
I'm only a bit worried about your sentence "[...] in some situations you
might find that the evaluation of y begins before the evaluation of x".
What does it mean, practically?
<snip>
Practical effects: From the C programmer's perspective,
none, because the program operates "as if" x==0 completely
suppressed the evaluation of y. Someone studying the compiled
code might be confused by seeing some parts of y evaluated
early, but that's just a distraction. The only reason I brought
it up was in response to your phrase "for whatever purpose."

Thank you, your example was clear!

One more question, if I can...

What if "y" is a function modifying "x"?
Same "as if" rule holds. "y" is executed only if "x" evaluates true.
Can the compiler figure our
that evaluating "y" has effects on "x"?
Yes. An optimising compiler can.
In this case, is the function
not entered at all, or stopped before modifying "x"?
The function would most likely not be executed if "x" evaluates to
false. It makes no sense to enter the function but "stop" before
modifying "x".

Jul 9 '07 #7
Pietro Cerutti wrote On 07/09/07 09:02,:
Eric Sosman wrote:
>>Pietro Cerutti wrote:
>>>Eric Sosman wrote:

Pietro Cerutti wrote:

>Hi group,
>here I come with a question which is quite simple per se, but for which
>I can't find an answer.
>
>Does the C standard guarantee that inside an expression such as
>
>(x && y)
>
>"y" is not evaluated if "x" evaluates to 0?
[...]

One more question, if I can...

What if "y" is a function modifying "x"? Can the compiler figure our
that evaluating "y" has effects on "x"? In this case, is the function
not entered at all, or stopped before modifying "x"?
If x is zero, the program must behave as if y is
never evaluated at all. Any side-effects of evaluating
y -- like modifying x -- must not occur if x was zero
to begin with.

if (j 0 && --j 0) ...

If j is zero or negative before this test, --j will not
be evaluated and j's value will not change.

--
Er*********@sun .com
Jul 9 '07 #8
Richard <rg****@gmail.c omwrites:
Pietro Cerutti <ga**@gahr.chwr ites:
[...]
>One more question, if I can...

What if "y" is a function modifying "x"? Can the compiler figure our
that evaluating "y" has effects on "x"? In this case, is the function
not entered at all, or stopped before modifying "x"?
Lots of context snipped. An example of what we're now talking about is

if (x && y()) /* ... */
How or why would a compiler allow that function to be called and then
"stop it before modifying x"?
In any way it likes. For example, if the call to y is inlined, then
'x && y()' could become a single large expression not involving a
function call. The compiler can then manipulate this large expression
in various ways, as long as the required semantics are not violated.
In particular, the sequence points are still there, and the compiler
has to be very careful about moving code across sequence points.
y is not evaluated if x is true.
You mean "if x is false" (more precisly, if x is equal to zero).

That's true in the abstract machine. It's not necessarily true in the
actual generated code, as long as the code behaves *as if* y is not
evaluated if x is true.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 9 '07 #9
Keith Thompson <ks***@mib.orgw rites:
Richard <rg****@gmail.c omwrites:
>Pietro Cerutti <ga**@gahr.chwr ites:
[...]
>>One more question, if I can...

What if "y" is a function modifying "x"? Can the compiler figure our
that evaluating "y" has effects on "x"? In this case, is the function
not entered at all, or stopped before modifying "x"?

Lots of context snipped. An example of what we're now talking about is

if (x && y()) /* ... */
>How or why would a compiler allow that function to be called and then
"stop it before modifying x"?

In any way it likes. For example, if the call to y is inlined, then
'x && y()' could become a single large expression not involving a
function call. The compiler can then manipulate this large expression
in various ways, as long as the required semantics are not violated.
In particular, the sequence points are still there, and the compiler
has to be very careful about moving code across sequence points.
>y is not evaluated if x is true.

You mean "if x is false" (more precisly, if x is equal to zero).
*blush* Sorry. Obvious typo.
>
That's true in the abstract machine. It's not necessarily true in the
actual generated code, as long as the code behaves *as if* y is not
evaluated if x is true.
In which case, to simplify things since the observed results are the
same, maybe better to say "is not executed". Not everything needs to be
caveated IMO - it leads to confusion for nOObs.

--
Jul 10 '07 #10

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

Similar topics

24
3363
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I see that generator expressions have been added to the language with 2.4 and I question the need for it. I know that it allows for lazy evaluation which speeds things up for larger lists but why was it necessary to add it instead of improving list...
2
4132
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two days! Can anyone point me to good resources (or implementations) that do this. Basically is there any way to optimize a boolean expression expressed in RPN (reverse polish notation)? I want to implement the algorithm/data structure in C. If you...
2
1785
by: Jerry LeVan | last post by:
I have an srf sql function "annual_report(<year>)" that as 14 columns, a category, 12 month numeric columns, and a total numeric column. The function finds monthly totals for each category (which is the target of "order by") and the grand total for the specified year. I have another sql function annual_profit_loss(<year>) that summarizes the total
8
1923
by: birchb | last post by:
While working on type expressions I am rather stuck for a way to express recursive types. A simple example of this is a singly-linked list of integers. In some languages you have compiler syntax which suspends evaluation so you can have recursive types. e.g. typedef Linked_List := int, Linked_List In LISP I would use a macro.
0
9887
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10983
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10648
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10707
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7883
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4529
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4119
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.