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

Undefined

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.

Jun 16 '06 #1
8 1744
mdh wrote:

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

--
pete
Jun 16 '06 #2
On 16 Jun 2006 10:29:37 -0700, "mdh" <md**@comcast.net> wrote:
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.
Jun 16 '06 #3
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....

Jun 16 '06 #4

mdh wrote:
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....


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

Jun 16 '06 #5
mdh

Tom St Denis wrote:
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".

Jun 16 '06 #6


mdh wrote On 06/16/06 14:01,:
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....


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.)

--
Er*********@sun.com

Jun 16 '06 #7
mdh

Eric Sosman wrote:
Rearrangements might be done for other reasons than
just to minimize the number of temporary results........


thank you Eric. Nice explanation.

Jun 16 '06 #8
pete wrote:
mdh wrote:

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


The safe way is:

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

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jun 16 '06 #9

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

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
1
by: Stu | last post by:
I am trying to build the xerces shared library with 2.3.0 version of their source code on an AIX 5.1 32 bit machine with the following version of the g++ compiler /usr/local/bin/g++ -v Reading...
4
by: Mike | last post by:
I am having a problem when a field is spaces being undefined. I wasn't sure if the problem was Excel or Javascript, so I thought I would post here first. The users are able to select from a drop...
1
by: Codemutant | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I just cannot find what is undefined in this code.
1
by: Foolster41 | last post by:
I'm rather new to C++ programing. I'm using the dev-C++ program on a windows XP OS. I'm trying to compile the code for a multi user dungeon (MUD) called circle-mud. When I compile I get the...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
4
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
3
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)':...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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
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: 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...

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.