Re: [avr-gcc-list] OT Generic C question
On Thu, 2005-09-22 at 19:12 +0100, Richard Urwin wrote: There is a sequence point immediately before the assignment. So, far from being a compiler bug, this is absolutely correct behaviour. Anything else would be a bug. 1) The value of i is taken (0) 2) i is incremented (to 1) sequence point--- 3) i is set to the value taken (0) Richard, Can you explain how you came to the conclusion that there is a sequence point before the assignment? Here's an excerpt from: http://publications.gbdirect.co.uk/c_book/chapter8/sequence_points.html This is why you cannot rely on expressions such as: a[i] = i++; because there is no sequence point specified for the assignment, increment or index operators, you don't know when the effect of the increment on i occurs. That section seems to imply that the compiler is NOT broken. I certainly may be reading this section incorrectly, or it may be subtly different from the original problem. I just want to understand a little more. -Alan -- - Alan Kilian kilian(at)bobodyne.com ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
On 9/20/05, Trampas [EMAIL PROTECTED] wrote: I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that i=i++ is somehow ambigous. The statement i++ means use the value of i then increment it, while ++i means increment i then use the value. Thus, there is no need to write i=i++. Use: i++; or ++i; or i = i+1; That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; So you guys are the smartest people I know when it comes to C so I thought I would ask you guys if this is a compiler bug or is my understanding of C just been shaken. Regards, Trampas Alex ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
Hi, On 20 Sep 2005 at 6:41, Trampas wrote: I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that I think that the right-hand side is completely evaluated before applying the result to the left-hand side, such that you could also think of it like this: i=0; tmp=i; i++; i = tmp; I do not know if this is compiler-dependent, but I would not count on the behaviour with this code. Regards, Wolfgang ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
RE: [avr-gcc-list] OT Generic C question
First off this was code written by a newbie to C and embedded development as such it would have been better to do i++ or i=i+1. But he did it as i=i++; The code was actually something like this: char name[20]; char c; int i; i=0; c=getchar(); while(c!=13) { name[i]=c; c=getchar(); i=i++; } name[i]=0; printf(%d, %s\n\r,i,name); Of course this code has overflow issues and such, but after the return character was seen the printf statement printed that i==0. Changing from i=i++; to i++; worked. Again my objective was to determine if this was a bug with compiler or not. From my understanding of the ++ operator this is indeed a bug. Sure it is bad coding but still a bug. Regards, Trampas -Original Message- From: David Brown [mailto:[EMAIL PROTECTED] Sent: Tuesday, September 20, 2005 7:48 AM To: Trampas; avr-gcc-list@nongnu.org Subject: Re: [avr-gcc-list] OT Generic C question - Original Message - From: Trampas [EMAIL PROTECTED] I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; So you guys are the smartest people I know when it comes to C so I thought I would ask you guys if this is a compiler bug or is my understanding of C just been shaken. Regards, Trampas I'd agree with you that i should be 1 after i = i++, despite the sillyness of the statement. However, how did you check that i was still 0? If the compiler had no reason to actually carry out the incrementation, and you used a debugger to view i, then it's quite likely that it remained at 0 due to legitimate compiler optomisations. ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
Hi Trampas, Thats why you have an post-increment operator and pre-increment operator. i=++i; will give you the result, you'd expected. i++, will execute the assignment and after that the value gets inc'd ++i, will inc'd and then assign Hopefully I got it right :) /Marc Trampas wrote: I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; So you guys are the smartest people I know when it comes to C so I thought I would ask you guys if this is a compiler bug or is my understanding of C just been shaken. Regards, Trampas ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
Trampas wrote: I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; So you guys are the smartest people I know when it comes to C so I thought I would ask you guys if this is a compiler bug or is my understanding of C just been shaken. Bad C. http://www.eskimo.com/~scs/C-faq/s3.html ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
Please pose C questions to a relevant medium, e.g. the programming questions list of the ACCU ( WWW.ACCU.org ) or one of the C newsgroups ( e.g. news:comp.lang.c or the C learners' newsgroup). On Tue, 20 Sep 2005 13:47:49 +0200, David Brown emailed: I'd agree with you that i should be 1 after i = i++, despite the sillyness of the statement. [..] it's quite likely that it remained at 0 due to legitimate compiler optomisations. I am not convinced that i should be 1 after i = i++ and I am not convinced that optimization is relevant. On Tue, 20 Sep 2005 13:35:13 +0200 Bernard Fouché emailed: Trampas wrote: I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; i++ means 'get i value, afterwards increment it'. So i=i++ will increment i, but after having read it to assign its value to itself :-) [..] I think that the rules of the value of the expression i++ might be different in C and Java, but in both languages, i=i++; means to assign the value of the expression i++ to i and in at least one of those languages, the value of the expression i++ is not equivalent to the sideeffect which i++ has on i. Please check for yourself what it is defined to be in C. ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
ya variable i should have increamentd to 1 ... but i guess u r not using incremented value of i in your program, so compiler thinks that it is of no use and doesn't do anything with the variable...you shoud try defining i variable ...then it will not optimise the code... and will show you the incremented value... - Original Message - From: Trampas [EMAIL PROTECTED] To: avr-gcc-list@nongnu.org Sent: Tuesday, September 20, 2005 4:11 PM Subject: [avr-gcc-list] OT Generic C question I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; So you guys are the smartest people I know when it comes to C so I thought I would ask you guys if this is a compiler bug or is my understanding of C just been shaken. Regards, Trampas ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list ***Confidentiality Notice*** The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain confidential or privileged information. If you are not the intended recipient, please notify the sender at Divinet or [EMAIL PROTECTED] immediately and destroy all copies of this message and any attachments. ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
Здравствуйте, Trampas. Вы писали 20 сентября 2005 г., 14:41:22: I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; So you guys are the smartest people I know when it comes to C so I thought I would ask you guys if this is a compiler bug or is my understanding of C just been shaken. Regards, Trampas ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list I think it is NOT a bug. Possibly this code is work in such way: 1. Internal variable (accumulator) is set equal to value of i. 2. Value of i is incremented. 3. Value of i is set equal to value of accumulator. This is possible logic of compiler and it is right. -- С уважением, Лев Выскубов mailto:[EMAIL PROTECTED] ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
From: David Brown [EMAIL PROTECTED] - Original Message - From: Trampas [EMAIL PROTECTED] I was helping a friend debug some code, he is new to C, using the Keil version of GCC for ARM. Anyway I found the following: int i; i=0; i=i++; //i was still zero that [...] I'd agree with you that i should be 1 after i = i++, despite the sillyness Actually, I think the naive interpretation would be that i should be zero after the above is executed: The statement implies three operations: 1) read the variable on the RHS (i) and save the value 2) increment the variable on the RHS (i). 3) store the value preserved in step 1 (0) in the variable on the LHS (i) The logical order of the operations is given above. In reality, the operations can occur in just about any order. The only guarantees the C standard makes about the order of execution involves a concept called sequence points. Sequence point occur in several places, but the most common is at the end of each full expression (as a first estimation, you can think of it as being at each semicolon). The standard says that the value of a variable should only be modified once between sequence points. The expression i = i++; attempts to modify i twice: once with the ++ operator and once with the = operator. Modifying a variable twice between sequence points is undefined behavior -- there is no correct answer. After the expression is executed, i could be 0, 1, 42, or the system might format your hard drive. All of the above and many other results are legal as far as the standard is concerned. As other have pointed out, the correct way to get the desired effect is one of ++i; i++; i += 1; i = i + 1; Each of which should result in identical code. Google on C sequence points for more information than you probably want. HTH, -=Dave ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
Hi, In C the result of this expression is undefined. The C language defines that the compiler is allowed to increment the variable i before or after the assignment operation. h=i++ is valid C, h will have the old value of i, i will be incremented. i=i++ is invalid C, the value of the variable i will be i or i+1 after execution of this expression, depending on the compiler. Regards, Nils int i; i=0; i=i++; //i was still zero that That is i=i++ never incremented i, now I would have thought the line would be the same as: i=i; i=i+1; ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
On 20 Sep 2005 at 9:59, Mike Murphree wrote: Alexandru Csete said: [...] int i = 3; i = i++; gave the values 3, 4 and 7 - each of them equally correct ;-) If writing software for a safety critical system which I occasionally do, I expect my compiler not to do Bad Things(TM) and it certainly violates POLA as well. Whether or not it is undefined behavior in the C standard makes little difference to me... What do you want to say with this? This is the bad code, the behaviour in this case is simply not defined, and if somebody writes such code in a safety critical system, he/she is definitely in the wrong place. Of course, one compiler should (and probably will) give the same result if compiled several times, but do not expect such crappy code to be portable. Mike Wolfgang ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
Re: [avr-gcc-list] OT Generic C question
On Sep 20, 2005, at 2:12 PM, Jeff Barlow wrote: Internet email is gradually degrading due to lame software and clueless users. Yes, that is exactly why I wrote. To alert the clueless. Apparently the clueless cherish their cluelessness, but that was nothing new. Yes, the messages are still threaded. Gmail has nothing on anything here. The point is that the messages are in the *wrong* thread. Its purely to the advantage of the poster to break a new topic out of an old thread and start fresh. Another advantage of proper threading is list archive search. Much more likely to find useful information when searching an archive which is properly threaded. Then again list archive searching is another lost art. On Sep 20, 2005, at 1:20 PM, Joerg Wunsch wrote: But in the end, people deploying that kind of laziness will simply cut theirselves off from people who might be willing to reply: If I kill an entire thread as I'm not interested in it, and deep in that thread appears a new question -- ce la vive. I won't be able to answer that new question as I won't see it at all. Some find it hard to believe others might tie their shoes differently. Same applies here as I expect many don't understand how what Joerg said above could be true. Many of us open this mailbox with threads collapsed under one entry. That old tired thread? Kill it. Next. Your query was deleted unseen because it was filed wrong. Because you sent it wrong. And I don't really care. However this needs to be mentioned now and then because sometimes The Clueless Get A Clue. I was split about 51/49 as to whether my posting this morning should continue in this thread or start a new. Decided that it was about this thread and therefore should stay in it. This way it was threaded directly to the message I was talking about if anyone cares to go read the message headers in the thread. And those who were already tired of this thread would not be bothered. -- David Kelly N4HHE, [EMAIL PROTECTED] Whom computers would destroy, they must first drive mad. ___ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list