435,158 Members | 917 Online
Need help? Post your question and get tips & solutions from a community of 435,158 IT Pros & Developers. It's quick & easy.

A popping question

 P: n/a 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 Replies

 P: n/a "mdh"

 P: n/a "mdh"

 P: n/a "mdh" "mdh" It doesn't matter. It's the compiler's job to reserve space fortemporaries used in expressions. The programmer doesn't have toworry 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

 P: n/a 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

 P: n/a Ben Pfaff wrote: "mdh" how does this work? ... >push ( pop() + pop()) ;I would have thought you would have to do something likedouble firstpop, secondpop;firstpop=pop();secondpop=pop();push ( firstpop+secondpop);ie where do the doubles go if they are not assigned. It obviously goessomewhere 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

 P: n/a CBFalconer "mdh" >how does this work? ... >>push ( pop() + pop()) ;I would have thought you would have to do something likedouble firstpop, secondpop;firstpop=pop();secondpop=pop();push ( firstpop+secondpop);ie where do the doubles go if they are not assigned. It obviously goessomewhere 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 fortemporaries used in expressions. The programmer doesn't have toworry 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

 P: n/a 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

 P: n/a On 21 Sep 2006 15:36:21 -0700, "mdh" Ben Pfaff wrote: >>The OP's question was about memory. You are talking about orderof evaluation. The K&R2 page in question mentions that - and /will not (reliably) do what we want for order of evaluation, sothere 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 hencethe 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 ofthings. I hope you meant to say "the lack of order". Remove del for email Sep 22 '06 #9

 P: n/a 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

 P: n/a On 21 Sep 2006 18:46:39 -0700, "mdh" Correct....I probably did think that any value had to be assigned hencethe 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 ofthings. Barry Schwarz wrote: >I hope you meant to say "the lack of order". Clearly carrying some deeper meaning, deeper than my knowledge. Careto 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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. 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". -- Simon. Sep 22 '06 #16

 P: n/a On 21 Sep 2006 23:59:48 -0700, "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 ofsense to use it in this manner. It removes unnecessary declarations ofint 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 functionfooand 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 tothe compiler and know it will workor2) I wonder what happens to the value of "pop()" before it is"pushed()" to the stack. Is there a working stack that the "pops" arepushed onto, then added then pushed back ....or is this merely someonelike me trying understand something that should not or cannot easilybe known...or is dependent upon the flavor of C being used hence (1) iswhat 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

 P: n/a On Fri, 22 Sep 2006 14:01:54 GMT, Simon Biber Barry Schwarz wrote: >The order of evaluation is at best implementation specified.Consequently, for a non commutative operator such as - or /, you don'tknow 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. Youclaimed to be aware of this but your terminology raised a doubt in mymind. You'd be best not to use "doubt" in its original meaning any more, sinceapparently over a billion "English" speakers have now replaced itsmeaning by that of "question". 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

 P: n/a Barry Schwarz San Diego Supercomputer Center <* We must do something. This is something. Therefore, we must do this. Sep 22 '06 #19

 P: n/a 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

 P: n/a On Sat, 23 Sep 2006 00:10:26 GMT, Keith Thompson wrote: >Barry Schwarz pop() was a function you introduced to start the discussion. From thecode you've shown, there is no push(). How the compiler chooses tohandle intermediate results while evaluating expressions is animplementation detail that will vary from system to system. My systemdoesn't have a hardware stack and intermediate results are stored intemporary variables which the compiler keeps track of (and attempts toreuse efficiently). Out of curiosity, what system are you using that doesn't have ahardware stack? It would be good to have a specific example in therecurring 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