473,396 Members | 2,011 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,396 software developers and data experts.

A popping question

mdh
Could someone help me understand one aspect of an exercise in K&R II;
(page 76)

Given function:

double pop( void){

....return a double from a stack if present

}

how does this work?

/* adds 2 doubles*/

case '+' :

push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop();
push ( firstpop+secondpop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

Thanks in advance.

Sep 21 '06 #1
20 1575
"mdh" <md**@comcast.netwrites:
how does this work?
....
push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop();
push ( firstpop+secondpop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.
It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.
--
"Programmers have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_
Sep 21 '06 #2
mdh

"mdh" <md**@comcast.netwrites:

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.
Ben Pfaff wrote:
It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.

So is it fair to assume that "pop" is in a sense replaced by it's
value, and any assignment is "additonal" but not necessary to the
execution of "pop"?

Sep 21 '06 #3
"mdh" <md**@comcast.netwrites:
>"mdh" <md**@comcast.netwrites:
ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

Ben Pfaff wrote:
>It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.

So is it fair to assume that "pop" is in a sense replaced by it's
value, and any assignment is "additonal" but not necessary to the
execution of "pop"?
Do you have an idea that the result of a function must be
immediately assigned to a variable, or that otherwise it is lost?
That is how I interpret your words. This idea is wrong. All of
the following are allowed in C, given an appropriate function foo
and variable x:
foo(); /* Throws away return value. */
x = foo(); /* Assigns return value. */
x = foo() + 1; /* Assigns return value plus 1. */
x = foo() + foo(); /* Calls foo twice, assigns the sum. */
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Sep 21 '06 #4
mdh

Ben Pfaff wrote:
Do you have an idea that the result of a function must be
immediately assigned to a variable, or that otherwise it is lost?
That is how I interpret your words. This idea is wrong.

no....I did not have an fixed idea...although it may have come across
that way...but your example clarifies it for me.

thanks.

Sep 21 '06 #5
Ben Pfaff wrote:
"mdh" <md**@comcast.netwrites:
>how does this work?
...
>push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop();
push ( firstpop+secondpop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.
Fine for + and *. Change it to - or / and things are different.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #6
CBFalconer <cb********@yahoo.comwrites:
Ben Pfaff wrote:
>"mdh" <md**@comcast.netwrites:
>>how does this work?
...
>>push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop();
push ( firstpop+secondpop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.

Fine for + and *. Change it to - or / and things are different.
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.
--
Just another C hacker.
Sep 21 '06 #7
mdh

Ben Pfaff wrote:
>
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.

Hi Ben,
Correct....I probably did think that any value had to be assigned hence
the question. I think, even in C, one has to take certain things as
"given" as your example clarified. And yes...I am aware of the order of
things.

Sep 21 '06 #8
On 21 Sep 2006 15:36:21 -0700, "mdh" <md**@comcast.netwrote:
>
Ben Pfaff wrote:
>>
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.


Hi Ben,
Correct....I probably did think that any value had to be assigned hence
the question. I think, even in C, one has to take certain things as
"given" as your example clarified. And yes...I am aware of the order of
things.
I hope you meant to say "the lack of order".
Remove del for email
Sep 22 '06 #9
mdh
Ben Pfaff wrote:
>
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.

Hi Ben,
Correct....I probably did think that any value had to be assigned hence
the question. I think, even in C, one has to take certain things as
"given" as your example clarified. And yes...I am aware of the order of
things.

Barry Schwarz wrote:
I hope you meant to say "the lack of order".
Clearly carrying some deeper meaning, deeper than my knowledge. Care
to explain?

Sep 22 '06 #10
On 21 Sep 2006 18:46:39 -0700, "mdh" <md**@comcast.netwrote:
>Correct....I probably did think that any value had to be assigned hence
the question. I think, even in C, one has to take certain things as
"given" as your example clarified. And yes...I am aware of the order of
things.


Barry Schwarz wrote:
>I hope you meant to say "the lack of order".

Clearly carrying some deeper meaning, deeper than my knowledge. Care
to explain?
The order of evaluation is at best implementation specified.
Consequently, for a non commutative operator such as - or /, you don't
know which operand will be evaluated first. In the code in question,
the operand was the result of a call to a stack popping function.
pop_1 - pop_2 produces a different result than pop_2 - pop_1. You
claimed to be aware of this but your terminology raised a doubt in my
mind.
Remove del for email
Sep 22 '06 #11
mdh

Barry Schwarz wrote:

The order of evaluation is at best implementation specified.
Consequently, for a non commutative operator such as - or /, you don't
know which operand will be evaluated first. In the code in question,
the operand was the result of a call to a stack popping function.
pop_1 - pop_2 produces a different result than pop_2 - pop_1. You
claimed to be aware of this but your terminology raised a doubt in my
mind.

And I see by my reply how this could have happened. My initial question
related to the issue of how "it worked" when and expression like

push( pop()+pop()) was evaluated.( I chose that example to avoid the
exact situation you showed.)

The answer I got explained that in the C language, it is permissible
to call a function and
--not doing anything with the result
--assign it to a variable
--assign the result to another function...which is still a little
puzzling to me..although I will have to accept the fact that the
compiler simply deals with it and it "just works".

Sep 22 '06 #12
mdh wrote:
The answer I got explained that in the C language, it is permissible
to call a function and
--not doing anything with the result
--assign it to a variable
--assign the result to another function...which is still a little
puzzling to me..although I will have to accept the fact that the
compiler simply deals with it and it "just works".
Note that the following is valid in C.

int *func1();
int func2();

*func1() = func2();

But I don't think this is what you meant by "assigning the result to
another function".
..

Sep 22 '06 #13

mdh wrote:
>
-snip-
push( pop()+pop()) was evaluated.( I chose that example to avoid the
exact situation you showed.)

The answer I got explained that in the C language, it is permissible
to call a function and
--not doing anything with the result
--assign it to a variable
--assign the result to another function...which is still a little
puzzling to me..although I will have to accept the fact that the
compiler simply deals with it and it "just works".
It works because this is how you have defined your function prototype.
If pop returns int and push accepts int argument, it makes lots of
sense to use it in this manner. It removes unnecessary declarations of
int type temp variables and other assignment statements.

-N

Sep 22 '06 #14
mdh
mdh wrote:
push( pop()+pop()) was evaluated.( I chose that example to avoid the
exact situation you showed.)

The answer I got explained that in the C language, it is permissible
to call a function and
--not doing anything with the result
--assign it to a variable
--assign the result to another function...which is still a little
puzzling to me..although I will have to accept the fact that the
compiler simply deals with it and it "just works".
Nishu wrote:
It works because this is how you have defined your function prototype.
If pop returns int and push accepts int argument, it makes lots of
sense to use it in this manner. It removes unnecessary declarations of
int type temp variables and other assignment statements.

I guess what I have been asking is this?

function:

push( pop()+pop());

If you are an experienced C programmer, how do you comprehend this?

Do you say...

1) OK...that's defined as such ( as Ben described--below)

All of the following are allowed in C, given an appropriate function
foo
and variable x:
foo(); /* Throws away return value. */
x = foo(); /* Assigns return value. */
x = foo() + 1; /* Assigns return value plus 1. */
x = foo() + foo(); /* Calls foo twice*/

To me as a C programmer, that's all I worry about...I leave the rest to
the compiler and know it will work

or

2) I wonder what happens to the value of "pop()" before it is
"pushed()" to the stack. Is there a working stack that the "pops" are
pushed onto, then added then pushed back ....or is this merely someone
like me trying understand something that should not or cannot easily
be known...or is dependent upon the flavor of C being used hence (1) is
what I am concerned about.

Well, I hope I am not driving people nuts...but, thats the way I try to
comprehend things.

Thanks for all your patience.

And I am slowly seeing why definitions in C are so important!!!! :-)

Sep 22 '06 #15
Barry Schwarz wrote:
The order of evaluation is at best implementation specified.
Consequently, for a non commutative operator such as - or /, you don't
know which operand will be evaluated first. In the code in question,
the operand was the result of a call to a stack popping function.
pop_1 - pop_2 produces a different result than pop_2 - pop_1. You
claimed to be aware of this but your terminology raised a doubt in my
mind.
<OT>

You'd be best not to use "doubt" in its original meaning any more, since
apparently over a billion "English" speakers have now replaced its
meaning by that of "question".

</OT>

--
Simon.
Sep 22 '06 #16
On 21 Sep 2006 23:59:48 -0700, "mdh" <md**@comcast.netwrote:
>
>mdh wrote:
push( pop()+pop()) was evaluated.( I chose that example to avoid the
exact situation you showed.)

The answer I got explained that in the C language, it is permissible
to call a function and
--not doing anything with the result
--assign it to a variable
--assign the result to another function...which is still a little
puzzling to me..although I will have to accept the fact that the
compiler simply deals with it and it "just works".

Nishu wrote:
>It works because this is how you have defined your function prototype.
If pop returns int and push accepts int argument, it makes lots of
sense to use it in this manner. It removes unnecessary declarations of
int type temp variables and other assignment statements.


I guess what I have been asking is this?

function:

push( pop()+pop());

If you are an experienced C programmer, how do you comprehend this?

Do you say...

1) OK...that's defined as such ( as Ben described--below)

All of the following are allowed in C, given an appropriate function
foo
and variable x:
foo(); /* Throws away return value. */
x = foo(); /* Assigns return value. */
x = foo() + 1; /* Assigns return value plus 1. */
x = foo() + foo(); /* Calls foo twice*/
And uses the result of the two calls as the operands of the + operator
which will then attempt to compute the sum and assign that value to x.

Alternately, the + operator takes to operands. Each operand is an
expression. The two expressions must be evaluated before the +
operator can do its thing. Whichever operand is evaluated first
happens to result in a call to foo. In the course of this evaluation,
the result returned by foo replaces the call as the expression is
evaluated. Ditto for the other operand which is evaluated second.

Consider
x = foo() + (foo() * 2);
When the second + operand is evaluated, the * expression requires both
foo() and 2 to be evaluated and then their product formed. We don't
know whether this call to foo is the first or second because we don't
know the order in which the + operands are evaluated.
>
To me as a C programmer, that's all I worry about...I leave the rest to
the compiler and know it will work

or

2) I wonder what happens to the value of "pop()" before it is
"pushed()" to the stack. Is there a working stack that the "pops" are
pushed onto, then added then pushed back ....or is this merely someone
like me trying understand something that should not or cannot easily
be known...or is dependent upon the flavor of C being used hence (1) is
what I am concerned about.
pop() was a function you introduced to start the discussion. From the
code you've shown, there is no push(). How the compiler chooses to
handle intermediate results while evaluating expressions is an
implementation detail that will vary from system to system. My system
doesn't have a hardware stack and intermediate results are stored in
temporary variables which the compiler keeps track of (and attempts to
reuse efficiently).
Remove del for email
Sep 22 '06 #17
On Fri, 22 Sep 2006 14:01:54 GMT, Simon Biber <ne**@ralmin.ccwrote:
>Barry Schwarz wrote:
>The order of evaluation is at best implementation specified.
Consequently, for a non commutative operator such as - or /, you don't
know which operand will be evaluated first. In the code in question,
the operand was the result of a call to a stack popping function.
pop_1 - pop_2 produces a different result than pop_2 - pop_1. You
claimed to be aware of this but your terminology raised a doubt in my
mind.

<OT>

You'd be best not to use "doubt" in its original meaning any more, since
apparently over a billion "English" speakers have now replaced its
meaning by that of "question".

</OT>
Substituting disbelief, uncertainty, or question for doubt in this
sentence would still get the same point across.
Remove del for email
Sep 22 '06 #18
Barry Schwarz <sc******@doezl.netwrites:
[...]
pop() was a function you introduced to start the discussion. From the
code you've shown, there is no push(). How the compiler chooses to
handle intermediate results while evaluating expressions is an
implementation detail that will vary from system to system. My system
doesn't have a hardware stack and intermediate results are stored in
temporary variables which the compiler keeps track of (and attempts to
reuse efficiently).
Out of curiosity, what system are you using that doesn't have a
hardware stack? It would be good to have a specific example in the
recurring argument about whether C defines a "stack".

--
Keith Thompson (The_Other_Keith) 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.
Sep 22 '06 #19
mdh

mdh wrote:

I guess what I have been asking is this?

If you are an experienced C programmer, how do you comprehend this?

Do you say...

1) OK...that's defined as such ( as Ben described--below)

or

2) I wonder what happens to the value of "pop()" before it is
"pushed()" to the stack.

And I am slowly seeing why definitions in C are so important!!!! :-)

Barry Schwarz wrote:
pop() was a function you introduced to start the discussion. How the
compiler chooses to
handle intermediate results while evaluating expressions is an
implementation detail that
>>>>>>>
Mdh replies:
Thanks for our reply. That helps clear it up for me.

Sep 22 '06 #20
On Sat, 23 Sep 2006 00:10:26 GMT, Keith Thompson <ks***@mib.org>
wrote:
>Barry Schwarz <sc******@doezl.netwrites:
[...]
>pop() was a function you introduced to start the discussion. From the
code you've shown, there is no push(). How the compiler chooses to
handle intermediate results while evaluating expressions is an
implementation detail that will vary from system to system. My system
doesn't have a hardware stack and intermediate results are stored in
temporary variables which the compiler keeps track of (and attempts to
reuse efficiently).

Out of curiosity, what system are you using that doesn't have a
hardware stack? It would be good to have a specific example in the
recurring argument about whether C defines a "stack".
An IBM Multiprise 2003 S/390 running OS/390 2.10.
Remove del for email
Sep 23 '06 #21

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

Similar topics

2
by: Obscurr | last post by:
hi! I was wondering how I could send a mail without the mail client popping up to the user. I would like a mail to be sent to myself when someone's pushing a button on my homepage. Any ideas? ...
17
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows...
4
by: ~Gee | last post by:
Hi, When I try to compile the following program, I get the following error: $ g++ anotherTest.C anotherTest.C: In function `int main()': anotherTest.C:47: void value not ignored as it ought to...
3
by: Lee David | last post by:
I'm trying to have a popup window of new changes when a person comes to my web page and something has changed. I can see the "alert" popping up, but not the page. The page would look better and...
3
by: Bill Nguyen | last post by:
Once applied the latest security patches from Microsoft, OL on our VB app client machines keep popping up dialog box asking for permission to send mail using OL. Is there anyway to by pass this...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
3
by: Learner | last post by:
Hello, I have two buttons on one of my VehicleDetails.aspx page. Obiviously these two buttons takes the user to two different pages. Now my client is interested in having a linkbutton instead of...
0
by: ray007x | last post by:
I have a question for Beth Melton. Hello. This is Ray. I read an answer you graciously gave to another user about the constant popping up of "Windows Installer - Preparing to install". I cannot...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.