Undefined

M

mdh

May I ask a question related to one I just posted. I am doing external
variables in K&R. As an example, the construction of an RPN calculator
is considered. One of the executions involves finding the difference
between 2 "popped" values from a stack. It is noted that the following
code is incorrect, as the order "in which the two calls of pop are
evaluated is not defined"

namely:

push(pop()-pop());

Could anyone elaborate on this for me? What exactly is meant by this,
and why? Surely if I pop a value from an array, say at position 'x' and
then pop the next value at '(x-1)' and then perform an execution of
x-(x-1), why would this not perform as expected? Or is there something
a lot more subtle that I am missing.

Thank you all as always.
 
P

pete

mdh said:
May I ask a question related to one I just posted. I am doing external
variables in K&R. As an example, the construction of an RPN calculator
is considered. One of the executions involves finding the difference
between 2 "popped" values from a stack. It is noted that the following
code is incorrect, as the order "in which the two calls of pop are
evaluated is not defined"

namely:

push(pop()-pop());

Could anyone elaborate on this for me? What exactly is meant by this,
and why? Surely if I pop a value from an array,
say at position 'x' and
then pop the next value at '(x-1)' and then perform an execution of
x-(x-1), why would this not perform as expected?
Or is there something
a lot more subtle that I am missing.

For (A - B), it's possible that B can be evaluated first.
You could have either
x-(x-1)
or
(x-1)-x
 
R

Roberto Waltman

May I ask a question related to one I just posted. I am doing external
variables in K&R. As an example, the construction of an RPN calculator
is considered. One of the executions involves finding the difference
between 2 "popped" values from a stack. It is noted that the following
code is incorrect, as the order "in which the two calls of pop are
evaluated is not defined"

namely:

push(pop()-pop());

Could anyone elaborate on this for me? What exactly is meant by this,
and why? Surely if I pop a value from an array, say at position 'x' and
then pop the next value at '(x-1)' and then perform an execution of
x-(x-1), why would this not perform as expected? Or is there something
a lot more subtle that I am missing.

You are assuming that the call to pop() at the left of the '-' sign
will be performed before calling the pop() at the right side.
The C language does not guarantee that ordering.
 
M

mdh

You are assuming that the call to pop() at the left of the '-' sign
will be performed before calling the pop() at the right side.
The C language does not guarantee that ordering.


Maybe this is not the type of useless question one normally asks, but
why not? There must be some subtle?? interesting reason why it is not
guaranteed? Would it not make life a lot easier if it were.? Just
curious....
 
T

Tom St Denis

mdh said:
Maybe this is not the type of useless question one normally asks, but
why not? There must be some subtle?? interesting reason why it is not
guaranteed? Would it not make life a lot easier if it were.? Just
curious....

It isn't part of the grammar that's why.

a + b

doesn't mean "load a, load b, add" it means "we are adding two symbols
together, a, b".

Tom
 
M

mdh

Tom said:
It isn't part of the grammar that's why.

a + b

doesn't mean "load a, load b, add" it means "we are adding two symbols
together, a, b".



OK...thanks...when one is starting off, there are things like this that
beg for an explanation. I imagine after a while, those become
non-issues as one just "does it".
 
E

Eric Sosman

mdh wrote On 06/16/06 14:01,:
Maybe this is not the type of useless question one normally asks, but
why not? There must be some subtle?? interesting reason why it is not
guaranteed? Would it not make life a lot easier if it were.? Just
curious....

Sometimes the compiler can generate faster or smaller
code if it has the freedom to rearrange an expression.
Let's take a case like

x = a * b + (c + d) / (e + f);

Somewhere before forming the final sum and storing it in x,
the computer will need to calculate various intermediate
results: a product, two sums, and a quotient. Right? Okay,
how many of those intermediate results must the computer
keep track of simultaneously?

If we evaluate left-to-right, we get

a * b (one intermediate)
c + d (another intermediate)
e + f (a third intermediate)
quotient (back down to two)
sum (just one remains)
store to x (done)

.... so the compiler needs to find places to hold three
partial results. But if it rearranges things, it can do

c + d (one intermediate)
e + f (another intermediate)
quotient (back down to one)
a * b (back up to two)
sum (back down to one)
store x (done)

.... using only two slots for intermediate results. This
allows the compiler to use the "saved" slot for some other
purpose, like holding the value of the variable y that you
just calculated a moment ago and are about to use in the
next line.

Rearrangements might be done for other reasons than
just to minimize the number of temporary results that must
be kept track of, too, but that's a topic for another day.
(And probably for another newsgroup.)
 
C

CBFalconer

pete said:
For (A - B), it's possible that B can be evaluated first.
You could have either
x-(x-1)
or
(x-1)-x

The safe way is:

tmp = pop();
push(tmp - pop());
 

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
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top