473,382 Members | 1,622 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,382 software developers and data experts.

order of execution

I wonder if something like this
would be save/portable:

m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
...

The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?
copx

Nov 13 '05 #1
9 4329
copx <in*****@invalid.com> scribbled the following:
I wonder if something like this
would be save/portable: m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
.. The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?


Yes it is guaranteed.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Insanity is to be shared."
- Tailgunner
Nov 13 '05 #2
In 'comp.lang.c', "copx" <in*****@invalid.com> wrote:
I wonder if something like this
would be save/portable:

m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
..

The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?


Yes it is.

Assuming 'p' is a pointer, an expression like

if (p && *p)
{
...

is idiomatic.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3
av
Joona I Palaste wrote:
copx <in*****@invalid.com> scribbled the following:
I wonder if something like this
would be save/portable:

m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
..

The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?


Yes it is guaranteed.


Is this something that is a defined behaviour of the ANSI C compiler
standard?

I can think of compilers, which might decide to optimize the above code,into
slightly different assmembly code, when heavy optimization is turned on.

Thanks,
AV.
Nov 13 '05 #4
av <va*****@iit.edu> scribbled the following:
Joona I Palaste wrote:
copx <in*****@invalid.com> scribbled the following:
I wonder if something like this
would be save/portable:
m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
..

The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?


Yes it is guaranteed.

Is this something that is a defined behaviour of the ANSI C compiler
standard? I can think of compilers, which might decide to optimize the above code,into
slightly different assmembly code, when heavy optimization is turned on.


Yes it is defined behaviour by the ISO (ANSI) C standard. It is not a
compiler-specific optimisation feature.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Nothing lasts forever - so why not destroy it now?"
- Quake
Nov 13 '05 #5
In article <vt*******************@rwcrnsc51.ops.asp.att.net >,
av <va*****@iit.edu> wrote:

First of all, my newsreader says you set follow-up to "Re: order of
execution". Please try to avoid such nonsense in the future.
Joona I Palaste wrote:
copx <in*****@invalid.com> scribbled the following:
I wonder if something like this
would be save/portable:

m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
..

The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?


Yes it is guaranteed.


Is this something that is a defined behaviour of the ANSI C compiler
standard?

I can think of compilers, which might decide to optimize the above code,into
slightly different assmembly code, when heavy optimization is turned on.

There are two rules for a compiler: First, it has to do everything
exactly as the C Standard says. In this example, the right hand side of
an && must _not_ be evaluated if the left hand side is false. Second,
the compiler can cheat as much as it likes if you can't tell the
difference.

An example:

if (x > 0 && x < 10) ...

Strictly speaking, the compiler must test (x > 0) first. If that is
false, then it must not test (x < 10). However, you could never see the
difference. So the compiler is allowed in this case to test (x < 10)
first or test both conditions simultaneously. You will still get exactly
the same result as if the compiler had gone by the rules.

A second example:

int * p; ...;

if (p != NULL && *p == 10)...

Strictly speaking, the compiler must not test (*p == 10) if p is a null
pointer. Lets say use a computer that would crash when testing *p == 10
if p is NULL. So obviously you _can_ tell the difference if the compiler
cheats, therefore the compiler is not allowed to cheat.

There are computers where testing (*p == 10) would just produce a result
of true or false, even if p is a null pointer. On such a computer, the
compiler would be allowed to cheat, because you couldn't tell the
difference. And it is ok, exactly because you cannot tell the
difference.

Now there might be a computer where it is guaranteed by the hardware
that reading *p will give a value of 0 if p is a null pointer. On such a
computer, the compiler would be allowed to change the whole test to "if
(*p == 10)". Again, you couldn't tell the difference.

So altogether, the compiler can do whatever it likes, as long as it
guarantees that you get exactly the results that you should get
according to the C Standard. In other words, you will get the right
result.
Nov 13 '05 #6
copx <in*****@invalid.com> wrote:
I wonder if something like this
would be save/portable:

m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
..

The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?


Yes, according to the standard. Of course, there is no accounting for
compiler bugs.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #7
av <va*****@iit.edu> wrote:
# > Yes it is guaranteed.
#
# Is this something that is a defined behaviour of the ANSI C compiler
# standard?
#
# I can think of compilers, which might decide to optimize the above code,into
# slightly different assmembly code, when heavy optimization is turned on.

The serialisation is defined by the standard. However the compiler rearranges
the code, it cannot have side effects out of the serialisation order. If code
is executed out of serialisation order, the compiler must insure the side
effects (like the order of stores and loads or traps or exceptions) obey
serialisation constraints. So the compiler can begin an load early, before
the subscript guard is evaluated and verified, but if the guard fails and
the load generates an address error, the compiler must insure the error
is suppressed.

Similarly if the guard has a side effect which changes the subscript or
the subscripted element, an early load would have to be nullified and the
correct array element loaded.

Some hardware allows speculative execution with traps and stores held until
it decided that speculative execution is accepted. Also if a guarded exception
can be shown that it will happen in subsequent unguarded code, the code
can be moved in front of the guard with an early exception.

if (0<=m && m<upperbound && A[m]==quark) xyz();
gloop = A[m];

Whether the guard (0<=m && m<upperbound) is true or false, A[m] will still
be loaded. If it is known xyz() changes neither A nor m, the compiler
might load A[m] before the if-statement; if that is an address error, the
same exception would occur regardless of whether the guard is verified. The
location of the exception can change.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no respect for people with no shopping agenda.
Nov 13 '05 #8
Christian Bau wrote:
An example:

if (x > 0 && x < 10) ...

Strictly speaking, the compiler must test (x > 0) first. If that is
false, then it must not test (x < 10). However, you could never see the
difference. So the compiler is allowed in this case to test (x < 10)
first or test both conditions simultaneously. You will still get exactly
the same result as if the compiler had gone by the rules.

A second example:

int * p; ...;

if (p != NULL && *p == 10)...

Strictly speaking, the compiler must not test (*p == 10) if p is a null
pointer. Lets say use a computer that would crash when testing *p == 10
if p is NULL. So obviously you _can_ tell the difference if the compiler
cheats, therefore the compiler is not allowed to cheat.


Your point is understood, but the last example is a bit misleading.
The exact same "optimizations" can be in the last case as in the first
one as long as dereferencing a null pointer on that particular platform
is well behaved. The compiler does not quite follow the same rules as
the programmer. A better example of where you can tell the difference
and so the compiler is not allowed to performs such tricks is:

if(p < 0 && p++ < 0)...

Since if a compiler can optimize code by dereferencing a null pointer
under the covers there is nothing wrong with that as it is not bound
by the standard in quite the same way as the programmer. Dereferencing
a null pointer should be seen as a metaphor for some arbitrary
undefined behaviour.

--
Thomas.
"What is the future c existence which does not do in all languages"

Nov 13 '05 #9
"copx" <in*****@invalid.com> writes:
I wonder if something like this
would be save/portable:

m = find_slot();
if (m != NOT_FOUND && list[m].state == OK) {
..

The question is: Is it guaranteed
that the "list[m].state == OK" check
is NOT executed if the "m != NOT_FOUND"
is FALSE?


Yes, it is guaranteed.

-Micah
Nov 13 '05 #10

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
13
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
23
by: roman | last post by:
Hi, I would like to have two actions for one event. But I want the second action to trigger when the first one action completes. Is it possible to do this in javascript? I'm using the onclick...
42
by: Sabiyur | last post by:
Hi all, one of the recent post gives the macro to do swap #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp) This macro will work, if the execution is from left to right. That is step 1)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.