Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A question for this group...

Reply
Thread Tools

A question for this group...

 
 
Old Wolf
Guest
Posts: n/a
 
      05-16-2012
On May 17, 2:08*am, (E-Mail Removed) (Kenny McCormack)
wrote:
> But the C results defy logic (at least to me), and I'd be interested to find
> out why this happens.


You can set your compiler to output the assembly code,
which will give you some insight. As to why the compiler
outputs that assembly code, surely you will agree that
that is a question about compilers (and your specific
compiler).
 
Reply With Quote
 
 
 
 
Stephen Sprunk
Guest
Posts: n/a
 
      05-16-2012
On 16-May-12 05:53, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Tuesday, May 15, 2012 6:41:01 PM UTC+1, Kenny McCormack wrote:
> ... More dangerous than writing code like
>
> printf("%d %d %d",a++,a++,++a);
> ...
> For instance *I'd* expect a straitforward implementation to produce "7 6 6".
> But I see I was wrong. How do you explain the output observed? Visual C++
> produces this output.


What output?

I get "7 6 8" with my gcc. Interestingly, at -O1 and above, it moves
those constants directly onto the stack. The assembly generated with no
optimization is not very instructive at first. However, changing the
fourth argument from "++a" to "a++", to be like the second and third,
gives this:

movl $5, -8(%ebp) ; a = 5
movl -8(%ebp), %ecx ; 4th arg
addl $1, -8(%ebp) ; postinc 4th arg
movl -8(%ebp), %eax ; 3rd arg
addl $1, -8(%ebp) ; postinc 3rd arg
movl -8(%ebp), %edx ; 2nd arg
addl $1, -8(%ebp) ; postinc 2nd arg
movl %ecx, 12(%esp)
movl %eax, 8(%esp)
movl %edx, 4(%esp)
movl $.LC0, (%esp)
call printf

Now I think I see what GCC was doing with the original version:

movl $5, -8(%ebp) ; a = 5
addl $1, -8(%ebp) ; preinc 4th arg
movl -8(%ebp), %ecx ; 3rd arg
addl $1, -8(%ebp) ; postinc 3rd arg
movl -8(%ebp), %edx ; 2nd arg
addl $1, -8(%ebp) ; postinc 2nd arg
movl -8(%ebp), %eax ; 4th arg
movl %eax, 12(%esp)
movl %ecx, 8(%esp)
movl %edx, 4(%esp)
movl $.LC0, (%esp)
call printf

It looks like GCC does any preincrements, then takes the value of
postincremented arguments, then does any postincrements, then takes the
value of preincremented args. Weird--but legal.

> I suppose we could explain /why/ this has been left as UB. And discuss
> aliasing and sequence points and so on, but does this really help the OP?


I don't know about this particular OP, or any others for that matter,
but seeing such responses--and wondering what they meant and why C has
so many undefined, dusty corners--has definitely helped _me_ learn more
about C and write better code.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
 
 
 
Robert Miles
Guest
Posts: n/a
 
      06-24-2012
On 5/15/2012 12:41 PM, Kenny McCormack wrote:
> If somebody asks:
>
> Why does it hurt when I stick my finger in a light socket?
>
> Is it universally true that the only possible answer is:
>
> Don't stick your fingers in light sockets.


Depends on what else has been placed in the light socket, such as
sharp tacks or broken glass.


 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      06-25-2012
בתאריך יום שלישי, 15 במאי 2012 18:41:01 UTC+1, מאת Kenny McCormack:
> If somebody asks:
>
> Why does it hurt when I stick my finger in a light socket?
>
> Is it universally true that the only possible answer is:
>
> Don't stick your fingers in light sockets.
>

I could explain that if you're wearing leather shoes the electricity will find earth and electrocute you, whilst if you're wearing rubber shoes you'llbe alright.

That's a reasonable explanation for a teenager, a completely irresponsible thing to tell a young child.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      06-25-2012
In article <(E-Mail Removed)>,
Malcolm McLean <(E-Mail Removed)> wrote:
>בתאריך יום שלישי, 15 במאי 2012 18:41:01 UTC+1, מאת
>Kenny McCormack:
>> If somebody asks:
>>
>> Why does it hurt when I stick my finger in a light socket?
>>
>> Is it universally true that the only possible answer is:
>>
>> Don't stick your fingers in light sockets.
>>

>I could explain that if you're wearing leather shoes the electricity
>will find earth and electrocute you, whilst if you're wearing rubber
>shoes you'll be alright.
>
>That's a reasonable explanation for a teenager, a completely
>irresponsible thing to tell a young child.


Indeed. And I think that's what is so maddening about this newsgroup - that
all the regs assume that the non-regs (the "endless supply of newbies") are
morons/idiots/children. You can tell from the tone of all of their postings
(when they are not being outright insulting) that they feel they are
addressing sub-morons. Much like Mitt Romney and the rest of the GOP.

--
Is God willing to prevent evil, but not able? Then he is not omnipotent.
Is he able, but not willing? Then he is malevolent.
Is he both able and willing? Then whence cometh evil?
Is he neither able nor willing? Then why call him God?
~ Epicurus

 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      06-26-2012
On Monday, June 25, 2012 5:29:30 PM UTC+1, Kenny McCormack wrote:
> In article <(E-Mail Removed)>,
> Malcolm McLean <(E-Mail Removed)> wrote:
> >בתאריך יום שלישי, 15 במאי 2012 18:41:01 UTC+1, מאת
> >Kenny McCormack:


> >> If somebody asks:
> >>
> >> Why does it hurt when I stick my finger in a light socket?
> >>
> >> Is it universally true that the only possible answer is:
> >>
> >> Don't stick your fingers in light sockets.


this is a poor analogy. We're actually talking about code like
printf("%d %d %d",a++,a++,++a);
and the responses *aren't* "Don't Do That". Its "thats undefined behaviour,its outcome can't be usefyully predicted". IMHO analysing code like the above for a particular compiler is time wasted. Not just for the naive and childish poster (as you insist we treat them) but for anyone.

> >I could explain that if you're wearing leather shoes the electricity
> >will find earth and electrocute you, whilst if you're wearing rubber
> >shoes you'll be alright.


well I'll let you try this first with 240V 30A (european wall current). Howabout 415V? What about the inside of of a CRT? I'm a firm beliver in keeping my hands in my pockets around large voltages and currents.

> >That's a reasonable explanation for a teenager, a completely
> >irresponsible thing to tell a young child.

>
> Indeed. And I think that's what is so maddening about this newsgroup - that
> all the regs assume that the non-regs (the "endless supply of newbies") are
> morons/idiots/children.


nonsense. I learned a lot on this ng and I hope other people can too.

> You can tell from the tone of all of their postings
> (when they are not being outright insulting) that they feel they are
> addressing sub-morons. Much like Mitt Romney and the rest of the GOP.

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      06-27-2012
In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:
>On Monday, June 25, 2012 5:29:30 PM UTC+1, Kenny McCormack wrote:
>> In article <(E-Mail Removed)>,
>> Malcolm McLean <(E-Mail Removed)> wrote:
>> >בתאריך יום שלישי, 15 במאי 2012 18:41:01 UTC+1, מאת
>> >Kenny McCormack:

>
>> >> If somebody asks:
>> >>
>> >> Why does it hurt when I stick my finger in a light socket?
>> >>
>> >> Is it universally true that the only possible answer is:
>> >>
>> >> Don't stick your fingers in light sockets.

>
>this is a poor analogy. We're actually talking about code like


I heard about this time, back in, I think it was, 1988, where someone on
Usenet used an analogy to try to prove some point, and his debating
opponents at the time did *not* retort that it was a "poor analogy".

Hasn't happened since, of course...

--
Here's a simple test for Fox viewers:

1) Sit back, close your eyes, and think (Yes, I know that's hard for you).
2) Think about and imagine all of your ridiculous fantasies about Barack Obama.
3) Now, imagine that he is white. Cogitate on how absurd your fantasies
seem now.

See? That wasn't hard, was it?

 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      06-27-2012
On Wednesday, June 27, 2012 10:05:44 AM UTC+1, Kenny McCormack wrote:
> In article <(E-Mail Removed)>,
> <(E-Mail Removed)> wrote:
> >On Monday, June 25, 2012 5:29:30 PM UTC+1, Kenny McCormack wrote:
> >> In article <(E-Mail Removed)>,
> >> Malcolm McLean <(E-Mail Removed)> wrote:
> >> >בתאריך יום שלישי, 15 במאי 2012 18:41:01 UTC+1, מאת
> >> >Kenny McCormack:


> >> >> If somebody asks:
> >> >>
> >> >> Why does it hurt when I stick my finger in a light socket?
> >> >>
> >> >> Is it universally true that the only possible answer is:
> >> >>
> >> >> Don't stick your fingers in light sockets.

> >
> >this is a poor analogy. We're actually talking about code like

>
> I heard about this time, back in, I think it was, 1988, where someone on
> Usenet used an analogy to try to prove some point, and his debating
> opponents at the time did *not* retort that it was a "poor analogy".


"..to prove some point.."
leaving aside that you don't you don't prove anything by analogy...
*what* point was being proved? Maybe the analogy was sound in that case.

But in any case I am not bound by others peoples past mistakes. clc generally *does* give a reason why it is a bad idea.

> Hasn't happened since, of course...


 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      06-27-2012
On 2012-06-26, (E-Mail Removed)
<(E-Mail Removed)> wrote:
>> >> Don't stick your fingers in light sockets.

>
> this is a poor analogy. We're actually talking about code like
>
> printf("%d %d %d",a++,a++,++a);
>
> and the responses *aren't* "Don't Do That". Its "thats
> undefined behaviour, its outcome can't be usefyully predicted".
> IMHO analysing code like the above for a particular compiler is
> time wasted. Not just for the naive and childish poster (as you
> insist we treat them) but for anyone.


While K&R sometimes urges learners to try out different
strategies to find out what their particular implementation does
with odd constructs, e.g., Exercise 1-2, in this case they agree
with the most common responses in this newsgroup.

They give the technical reason that constructs like this are
undefined, but they further say that knowing the result of
expressions like a[i] = ++i on your implementation is, in fact,
evil.

The moral is that writing code that depends on order of
evaluation is a bad programming practice in any language.
Naturally, it is necessary to know what things to avoid, but if
you don't know how they are done on various machines you won't
be tempted to take advantage of a particular implementation.

Do not eat of that fruit, my friends.

--
Neil Cerutti
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      06-28-2012
On Thursday, June 28, 2012 8:19:21 AM UTC+1, io_x wrote:

> "yes UBs are ok...
> it is question of master 1000 pages of standard for a language..."


I don't think the C standard is that large. Its a pretty slim and
readable docuemnt (not all language standards are). An most of the
UB is fairly obvious and that that isn't usually has a good reason.

> for me, i prefer a language without UBs


name one. C is actually pretty good.

> or if it is possible in a set
> more little it is possible...


don't understand that.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments