Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #20810 > unrolled thread

A question for this group...

Started bygazelle@shell.xmission.com (Kenny McCormack)
First post2012-05-15 17:41 +0000
Last post2012-07-06 17:43 -0700
Articles 20 on this page of 34 — 19 participants

Back to article view | Back to comp.lang.c


Contents

  A question for this group... gazelle@shell.xmission.com (Kenny McCormack) - 2012-05-15 17:41 +0000
    Re: A question for this group... "Chine Bleu, Russie Blanche, et Khmer Rouge" <chine.bleu@yahoo.com> - 2012-05-15 10:44 -0700
      Re: A question for this group... James Kuyper <jameskuyper@verizon.net> - 2012-05-15 14:06 -0400
    Re: A question for this group... William Ahern <william@wilbur.25thandClement.com> - 2012-05-15 13:24 -0700
      Re: A question for this group... Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2012-05-15 20:56 +0000
        Re: A question for this group... nick_keighley_nospam@hotmail.com - 2012-05-16 03:41 -0700
    Re: A question for this group... Old Wolf <oldwolf@inspire.net.nz> - 2012-05-16 02:31 -0700
    Re: A question for this group... nick_keighley_nospam@hotmail.com - 2012-05-16 03:53 -0700
      Re: A question for this group... gazelle@shell.xmission.com (Kenny McCormack) - 2012-05-16 14:08 +0000
        Re: A question for this group... Martin Shobe <martin.shobe@yahoo.com> - 2012-05-16 14:49 +0000
          Re: A question for this group... Martin Shobe <martin.shobe@yahoo.com> - 2012-05-16 14:59 +0000
        Re: A question for this group... Kaz Kylheku <kaz@kylheku.com> - 2012-05-16 16:05 +0000
        Re: A question for this group... Old Wolf <oldwolf@inspire.net.nz> - 2012-05-16 14:29 -0700
      Re: A question for this group... Stephen Sprunk <stephen@sprunk.org> - 2012-05-16 18:18 -0500
    Re: A question for this group... Robert Miles <milesrf@Usenet-News.net> - 2012-06-23 23:22 -0500
    Re: A question for this group... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-25 02:41 -0700
      Re: A question for this group... gazelle@shell.xmission.com (Kenny McCormack) - 2012-06-25 16:29 +0000
        Re: A question for this group... nick_keighley_nospam@hotmail.com - 2012-06-26 08:27 -0700
          Re: A question for this group... gazelle@shell.xmission.com (Kenny McCormack) - 2012-06-27 09:05 +0000
            Re: A question for this group... nick_keighley_nospam@hotmail.com - 2012-06-27 03:57 -0700
          Re: A question for this group... Neil Cerutti <neilc@norwich.edu> - 2012-06-27 13:49 +0000
            Re: A question for this group... "io_x" <a@b.c.invalid> - 2012-06-28 09:19 +0200
              Re: A question for this group... nick_keighley_nospam@hotmail.com - 2012-06-28 01:59 -0700
                Re: A question for this group... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-28 04:18 -0700
                  Re: A question for this group... Heikki Kallasjoki <fis+usenet@zem.fi> - 2012-06-28 12:14 +0000
                    Re: A question for this group... James Kuyper <jameskuyper@verizon.net> - 2012-06-28 08:28 -0400
                      Re: A question for this group... James Kuyper <jameskuyper@verizon.net> - 2012-06-28 08:33 -0400
                    Re: A question for this group... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-28 13:07 -0700
                      Re: A question for this group... David Thompson <dave.thompson2@verizon.net> - 2012-07-19 02:40 -0400
                Re: A question for this group... "io_x" <a@b.c.invalid> - 2012-06-28 18:58 +0200
                  Re: A question for this group... Andrew Cooper <amc96@cam.ac.uk> - 2012-06-29 01:11 +0100
                    Re: A question for this group... Philip Lantz <prl@canterey.us> - 2012-07-05 00:21 -0700
                      Re: A question for this group... nick_keighley_nospam@hotmail.com - 2012-07-06 00:24 -0700
                      Re: A question for this group... Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-06 17:43 -0700

Page 1 of 2  [1] 2  Next page →


#20810 — A question for this group...

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2012-05-15 17:41 +0000
SubjectA question for this group...
Message-ID<jou4fd$5uh$1@news.xmission.com>
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.

Is it really true that an explanation of how electricity works and how your
body is a conductor (and, in particular, how the real issue is how well
grounded your body is) is never appropriate?

This seems to be the attitude of this group - see in particular the recent
"Program Output" thread.

-- 
"Remember when teachers, public employees, Planned Parenthood, NPR and PBS
crashed the stock market, wiped out half of our 401Ks, took trillions in
TARP money, spilled oil in the Gulf of Mexico, gave themselves billions in
bonuses, and paid no taxes? Yeah, me neither."

[toc] | [next] | [standalone]


#20811

From"Chine Bleu, Russie Blanche, et Khmer Rouge" <chine.bleu@yahoo.com>
Date2012-05-15 10:44 -0700
Message-ID<chine.bleu-238E21.10442915052012@news.x-privat.org>
In reply to#20810
In article <jou4fd$5uh$1@news.xmission.com>,
 gazelle@shell.xmission.com (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.
> 
> Is it really true that an explanation of how electricity works and how your
> body is a conductor (and, in particular, how the real issue is how well
> grounded your body is) is never appropriate?
> 
> This seems to be the attitude of this group - see in particular the recent
> "Program Output" thread.

Doctor! Doctor! It hurts when I do this!

Then don't do that.

-- 
My name Indigo Montoya.      |    R'lyeh 38o57'6.5''S 102o51'16''E.
You flamed my father.        |       I'm whoever you want me to be.
Prepare to be spanked.       |  Annoying Usenet one post at a time.
Stop posting that!           |    At least I can stay in character.

[toc] | [prev] | [next] | [standalone]


#20812

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-05-15 14:06 -0400
Message-ID<4FB29B38.4070908@verizon.net>
In reply to#20811
On 05/15/2012 01:44 PM, Chine Bleu, Russie Blanche, et Khmer Rouge wrote:
> In article <jou4fd$5uh$1@news.xmission.com>,
>  gazelle@shell.xmission.com (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.
>>
>> Is it really true that an explanation of how electricity works and how your
>> body is a conductor (and, in particular, how the real issue is how well
>> grounded your body is) is never appropriate?
>>
>> This seems to be the attitude of this group - see in particular the recent
>> "Program Output" thread.
> 
> Doctor! Doctor! It hurts when I do this!
> 
> Then don't do that.

Bothering to respond to Kenny is usually a mistake, but why did you
bother making that response? It's just an example of what he's
complaining about.

If Kenny thinks that a more detailed answer is appropriate, he should
post one.

[toc] | [prev] | [next] | [standalone]


#20824

FromWilliam Ahern <william@wilbur.25thandClement.com>
Date2012-05-15 13:24 -0700
Message-ID<3lqa89-n19.ln1@wilbur.25thandClement.com>
In reply to#20810
Kenny McCormack <gazelle@shell.xmission.com> 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.
>
> Is it really true that an explanation of how electricity works and how
> your body is a conductor (and, in particular, how the real issue is how
> well grounded your body is) is never appropriate?
>
> This seems to be the attitude of this group - see in particular the recent
> "Program Output" thread.

Your light socket analogy is a perfect example. Anyone who has grown up in
an electrified civilization knows from an extremely early age that
electricity can be dangerous, and that a light socket is associated with
electricity . With this background knowledge, they would never ask "why does
it hurt when I do X" unless they were being lazy or naive. If he weren't
lazy or naive, he would have made the association with electricity and
reformed the question appropriately.

A common response to both laziness and naivety is sarcasm. It signals to the
person that they need to rethink their question in a very fundamental way;
that perhaps they're in deeper water than they think.

I went through the thread you mentioned (sans Google Groups posts). I think
the responses were just fine. They rightfully didn't talk down to the OP.
The responses were accurate, and they signaled to the OP that his analytical
investment in the issue was poor. If he can't interpret that signal, then
he's not prepared to learn anything.

[toc] | [prev] | [next] | [standalone]


#20826

FromQuentin Pope <qp19433@hotmail.NOSPAM.com>
Date2012-05-15 20:56 +0000
Message-ID<jouftb$203$1@speranza.aioe.org>
In reply to#20824
On Tue, 15 May 2012 13:24:35 -0700, William Ahern wrote:

> Kenny McCormack <gazelle@shell.xmission.com> 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.
>>
>> Is it really true that an explanation of how electricity works and how
>> your body is a conductor (and, in particular, how the real issue is how
>> well grounded your body is) is never appropriate?
>>
>> This seems to be the attitude of this group - see in particular the
>> recent "Program Output" thread.
> 
> Your light socket analogy is a perfect example. Anyone who has grown up
> in an electrified civilization knows from an extremely early age that
> electricity can be dangerous, and that a light socket is associated with
> electricity . With this background knowledge, they would never ask "why
> does it hurt when I do X" unless they were being lazy or naive. If he
> weren't lazy or naive, he would have made the association with
> electricity and reformed the question appropriately.
> 
> A common response to both laziness and naivety is sarcasm. It signals to
> the person that they need to rethink their question in a very
> fundamental way; that perhaps they're in deeper water than they think.
> 
> I went through the thread you mentioned (sans Google Groups posts). I
> think the responses were just fine. They rightfully didn't talk down to
> the OP. The responses were accurate, and they signaled to the OP that
> his analytical investment in the issue was poor. If he can't interpret
> that signal, then he's not prepared to learn anything.

I agree with this. Many posters to this group are lazy, ignorant, stupid 
or all three. There's only so much time and effort it's worth putting 
into helping someone swim, when natural selection seems to have chosen 
that person to sink.

[toc] | [prev] | [next] | [standalone]


#20851

Fromnick_keighley_nospam@hotmail.com
Date2012-05-16 03:41 -0700
Message-ID<21613267.875.1337164911915.JavaMail.geo-discussion-forums@vbx14>
In reply to#20826
On Tuesday, May 15, 2012 9:56:11 PM UTC+1, Quentin Pope wrote:
> On Tue, 15 May 2012 13:24:35 -0700, William Ahern wrote:
> 
> > Kenny McCormack <gazelle@shell.xmission.com> 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.
> >>
> >> Is it really true that an explanation of how electricity works and how
> >> your body is a conductor (and, in particular, how the real issue is how
> >> well grounded your body is) is never appropriate?
> >>
> >> This seems to be the attitude of this group - see in particular the
> >> recent "Program Output" thread.
> > 
> > Your light socket analogy is a perfect example. Anyone who has grown up
> > in an electrified civilization knows from an extremely early age that
> > electricity can be dangerous, and that a light socket is associated with
> > electricity . With this background knowledge, they would never ask "why
> > does it hurt when I do X" unless they were being lazy or naive. If he
> > weren't lazy or naive, he would have made the association with
> > electricity and reformed the question appropriately.
> > 
> > A common response to both laziness and naivety is sarcasm. It signals to
> > the person that they need to rethink their question in a very
> > fundamental way; that perhaps they're in deeper water than they think.
> > 
> > I went through the thread you mentioned (sans Google Groups posts). I
> > think the responses were just fine. They rightfully didn't talk down to
> > the OP. The responses were accurate, and they signaled to the OP that
> > his analytical investment in the issue was poor. If he can't interpret
> > that signal, then he's not prepared to learn anything.
> 
> I agree with this. Many posters to this group are lazy, ignorant, stupid 
> or all three. 

some are. Some aren't. I'm willing to give the benefit of the doubt. I thought the question quite reasonable. I've *written* code like that and been surprised at the result.

> There's only so much time and effort it's worth putting 
> into helping someone swim, when natural selection seems to have chosen 
> that person to sink.

[toc] | [prev] | [next] | [standalone]


#20850

FromOld Wolf <oldwolf@inspire.net.nz>
Date2012-05-16 02:31 -0700
Message-ID<7b4f49aa-842f-451f-8d64-275d55117873@nw7g2000pbb.googlegroups.com>
In reply to#20810
On May 16, 5:41 am, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> If somebody asks:
>         Why does it hurt when I stick my finger in a light socket?

Wow, how long have you been trolling this NG now,
8 years? Think the regs are suddenly going to see
the errors of their ways one day?

[toc] | [prev] | [next] | [standalone]


#20853

Fromnick_keighley_nospam@hotmail.com
Date2012-05-16 03:53 -0700
Message-ID<15497347.287.1337165607109.JavaMail.geo-discussion-forums@vbk4>
In reply to#20810
On Tuesday, May 15, 2012 6:41:01 PM UTC+1, Kenny McCormack wrote:

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

it might be interesting to discuss why it hurts. But it would also be a good idea to make sure the questioner knew this was a really dangerous thing to do. More dangerous than writing code like

printf("%d %d %d",a++,a++,++a); 

(I actually know someone who has a couple of fingers missing from sticking his fingers in an electrical socket.)

> Is it really true that an explanation of how electricity works and how your
> body is a conductor (and, in particular, how the real issue is how well
> grounded your body is) is never appropriate?
> 
> This seems to be the attitude of this group - see in particular the recent
> "Program Output" thread.

unsurprisingly I disagree. The C programmer language is defined by an abstract specification. Certain parts are left undefined. There the implementor has choice. It is meaningless to ask for a detailed explanation of the behaviour of a program that has strayed into such regions unless you also know a great deal about the particular implementation. With optimising compilers this could be really hard.

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.

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 know Kenny knows all this stuff but I thought the question was mildly interesting. Maybe a lurker will be enlightened.

[toc] | [prev] | [next] | [standalone]


#20857

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2012-05-16 14:08 +0000
Message-ID<jp0cc7$osc$1@news.xmission.com>
In reply to#20853
In article <15497347.287.1337165607109.JavaMail.geo-discussion-forums@vbk4>,
 <nick_keighley_nospam@hotmail.com> wrote:
...
>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.

The underlying point is that I think it *is* interesting to ask and to
answer why the OP gets the output that he gets.  I also get that result
(using gcc on a Linux system) and, TBH, I can't explain it.  Now, I know the
dogmatic position is that it doesn't matter what output you get, it doesn't
matter why you get the output you get, and it is a waste of time to even
think about thinking about why you get the output you get.  Well, the fact
is that this whole newsgroup is a splendid waste of time.  We're all wasting
time posting here - but we do it because it is fun.  Quite seriously, I
can't see how anyone could disagree with that assessment.

Further, to use my light socket analogy, I think everyone knows you
shouldn't stick your finger in it (except, apparently, your friend...), but
I seriously believe that most people - even well educated people - do not
really know *why* - do not really understand how electricity works.
Therefore, I think the "why" question is much more interesting than the
stock "Don't do that!" response.

Now, I took this code and ran it two different versions of AWK - and got
expected results.  GAWK gives:

 % gawk 'BEGIN {a=5;print a++,a++,++a}'
5 6 8

which is easily explainable as left-to-right evaluation.

TAWK gives 7 6 6, which is also easily explainable as right-to-left evaluation.
I would expect C (at least without its doing any optimization) to give the
same, since C usually uses "cdecl", which is right-to-left (of course, yes,
Keith, I understand, none of this is mandated by the C standards).

But the C results defy logic (at least to me), and I'd be interested to find
out why this happens.  Yes, Keith, we understand, it is all a waste of time,
but as noted above, we're here because the time is there to be wasted.

-- 
Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
an 8 bit operating system based on a 4 bit architecture from a 2 bit company
that can't stand 1 bit of competition.

Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
operating system based on a 4 bit architecture from a 2 bit company that
can't stand 1 bit of competition.

[toc] | [prev] | [next] | [standalone]


#20858

FromMartin Shobe <martin.shobe@yahoo.com>
Date2012-05-16 14:49 +0000
Message-ID<jp0epl$qao$1@dont-email.me>
In reply to#20857
Kenny McCormack wrote:

> In article <15497347.287.1337165607109.JavaMail.geo-discussion-forums@vbk4>,
>  <nick_keighley_nospam@hotmail.com> wrote:
> ...
>>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.
>
> The underlying point is that I think it *is* interesting to ask and to
> answer why the OP gets the output that he gets.  I also get that result
> (using gcc on a Linux system) and, TBH, I can't explain it.  Now, I know the
> dogmatic position is that it doesn't matter what output you get, it doesn't
> matter why you get the output you get, and it is a waste of time to even
> think about thinking about why you get the output you get.  Well, the fact
> is that this whole newsgroup is a splendid waste of time.  We're all wasting
> time posting here - but we do it because it is fun.  Quite seriously, I
> can't see how anyone could disagree with that assessment.
>
> Further, to use my light socket analogy, I think everyone knows you
> shouldn't stick your finger in it (except, apparently, your friend...), but
> I seriously believe that most people - even well educated people - do not
> really know *why* - do not really understand how electricity works.
> Therefore, I think the "why" question is much more interesting than the
> stock "Don't do that!" response.
>
> Now, I took this code and ran it two different versions of AWK - and got
> expected results.  GAWK gives:
>
>  % gawk 'BEGIN {a=5;print a++,a++,++a}'
> 5 6 8
>
> which is easily explainable as left-to-right evaluation.
>
> TAWK gives 7 6 6, which is also easily explainable as right-to-left evaluation.
> I would expect C (at least without its doing any optimization) to give the
> same, since C usually uses "cdecl", which is right-to-left (of course, yes,
> Keith, I understand, none of this is mandated by the C standards).
>
> But the C results defy logic (at least to me), and I'd be interested to find
> out why this happens.  Yes, Keith, we understand, it is all a waste of time,
> but as noted above, we're here because the time is there to be wasted.

While I can't say exactly what happened, the following would be a
reasonable compilation of the above into x86 assembly code, and would
result in that particular output.

Assembly      eax ebx ecx Note
-----------------------------------------
mov eax, a      5   ?   ? right              
inc eax         6   ?   ? right side effect
mov ebx, eax    6   6   ? middle
inc eax         7   6   ? middle side effect
mov ecx, ebx    7   6   7 left
inc eax         8   6   7 left side effect
push ecx                  right
push ebx                  middle
push eax                  left

Martin Shobe

[toc] | [prev] | [next] | [standalone]


#20859

FromMartin Shobe <martin.shobe@yahoo.com>
Date2012-05-16 14:59 +0000
Message-ID<jp0fbm$ugh$1@dont-email.me>
In reply to#20858
Martin Shobe wrote:

> Kenny McCormack wrote:
>
>> In article <15497347.287.1337165607109.JavaMail.geo-discussion-forums@vbk4>,
>>  <nick_keighley_nospam@hotmail.com> wrote:
>> ...
>>>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.
>>
>> The underlying point is that I think it *is* interesting to ask and to
>> answer why the OP gets the output that he gets.  I also get that result
>> (using gcc on a Linux system) and, TBH, I can't explain it.  Now, I know the
>> dogmatic position is that it doesn't matter what output you get, it doesn't
>> matter why you get the output you get, and it is a waste of time to even
>> think about thinking about why you get the output you get.  Well, the fact
>> is that this whole newsgroup is a splendid waste of time.  We're all wasting
>> time posting here - but we do it because it is fun.  Quite seriously, I
>> can't see how anyone could disagree with that assessment.
>>
>> Further, to use my light socket analogy, I think everyone knows you
>> shouldn't stick your finger in it (except, apparently, your friend...), but
>> I seriously believe that most people - even well educated people - do not
>> really know *why* - do not really understand how electricity works.
>> Therefore, I think the "why" question is much more interesting than the
>> stock "Don't do that!" response.
>>
>> Now, I took this code and ran it two different versions of AWK - and got
>> expected results.  GAWK gives:
>>
>>  % gawk 'BEGIN {a=5;print a++,a++,++a}'
>> 5 6 8
>>
>> which is easily explainable as left-to-right evaluation.
>>
>> TAWK gives 7 6 6, which is also easily explainable as right-to-left evaluation.
>> I would expect C (at least without its doing any optimization) to give the
>> same, since C usually uses "cdecl", which is right-to-left (of course, yes,
>> Keith, I understand, none of this is mandated by the C standards).
>>
>> But the C results defy logic (at least to me), and I'd be interested to find
>> out why this happens.  Yes, Keith, we understand, it is all a waste of time,
>> but as noted above, we're here because the time is there to be wasted.
>
> While I can't say exactly what happened, the following would be a
> reasonable compilation of the above into x86 assembly code, and would
> result in that particular output.
>
> Assembly      eax ebx ecx Note
> -----------------------------------------
> mov eax, a      5   ?   ? right              
> inc eax         6   ?   ? right side effect
> mov ebx, eax    6   6   ? middle
> inc eax         7   6   ? middle side effect
> mov ecx, ebx    7   6   7 left
> inc eax         8   6   7 left side effect
> push eax                  right
> push ebx                  middle
> push ecx                  left
>
> Martin Shobe

Oops, messed up the pushes. Fixed above.

Martin Shobe

[toc] | [prev] | [next] | [standalone]


#20860

FromKaz Kylheku <kaz@kylheku.com>
Date2012-05-16 16:05 +0000
Message-ID<20120516083446.523@kylheku.com>
In reply to#20857
On 2012-05-16, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In article <15497347.287.1337165607109.JavaMail.geo-discussion-forums@vbk4>,
> <nick_keighley_nospam@hotmail.com> wrote:
> ...
>>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.
>
> The underlying point is that I think it *is* interesting to ask and to
> answer why the OP gets the output that he gets.  I also get that result
> (using gcc on a Linux system) and, TBH, I can't explain it.  Now, I know the
> dogmatic position is that it doesn't matter what output you get, it doesn't
> matter why you get the output you get, and it is a waste of time to even
> think about thinking about why you get the output you get.

The point is that the newbie might not know that it's undefined (and may
also agree that it's a waste of time thinking about it once he knows
it is undefined).

The result is brought about by the same translation mechanism which brings
about well-defined results for well-defined code. That mechanism simply

> But the C results defy logic (at least to me), and I'd be interested to find
> out why this happens.  Yes, Keith, we understand, it is all a waste of time,
> but as noted above, we're here because the time is there to be wasted.

The C results are simply the compiler taking the liberties with regard
to function argument evaluation order. In the calling conventions on your
system, the leftmost argument goes to the top of the stack.

This means that, if push operations are used to prepare the arguments, the
rightmost argument value is pushed first.

(Even if it's not the case for non-variadic functions,  you will find
that variadic function calls preserve this tradition because variadic
argument passing in C grew out of a hack which depended on the
arguments going onto the stack in that order.)

So in

   a = 5;
   printf("%d %d %d", a++, a++, a++)

the compiler wants to push the results in right to left order. If the object a
is reliably updated after each argument expression is evaluated, then we
get 7 6 5.

One implication of this kind of stack based passing arrangement where the
leftmost argument is at the top of the stack is that excess arguments to
functions do not matter.

   printf("%d %d %d", 1, 2, 3, 4, 5);

The leftmost value is still at the top of the stack, where it is found by 
the first %d. You will find that this is quite broadly portable.

Anyway, the problem with our test case is that even if there is a stack which
requires the arguments to be pushed from right to left, the evaluation doesn't
have to happen that way.  The compiler could evaluate the three expressions in
a different order, save them in some temporary locations (or registers) and
then pass them in the required order.  And of course, the side effects of
updating a can be deferred, wreaking havoc. We are onlly able to see 7 6 5
because the side effect updates are interleaved into the evaluation.

However, we can fix the test case so that it has unspecified behavior, rather
than undefined behavior, by using functions:

   int fun(int *val)
   {
     return (*val)++;
   }

   /* ... */

   a = 5;
   printf("%d %d %d", fun(&a), fun(&a), fun(&a));

Now the increment is wrapped in sequence points inside the function.
Functions cannot be called in parallel in C, and so we have unspecified
behavior: there are exactly six possible evaluation orders for the three
function calls.  We will see the numbers 5, 6 and 7 in the output, in one of
six possible orders.

-- 
If you ever need any coding done, I'm your goto man!

[toc] | [prev] | [next] | [standalone]


#20866

FromOld Wolf <oldwolf@inspire.net.nz>
Date2012-05-16 14:29 -0700
Message-ID<3b7de305-78a5-4ba5-b725-54cd9365e0a1@nw7g2000pbb.googlegroups.com>
In reply to#20857
On May 17, 2:08 am, gaze...@shell.xmission.com (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).

[toc] | [prev] | [next] | [standalone]


#20868

FromStephen Sprunk <stephen@sprunk.org>
Date2012-05-16 18:18 -0500
Message-ID<jp1ck8$b9a$1@dont-email.me>
In reply to#20853
On 16-May-12 05:53, nick_keighley_nospam@hotmail.com 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

[toc] | [prev] | [next] | [standalone]


#22599

FromRobert Miles <milesrf@Usenet-News.net>
Date2012-06-23 23:22 -0500
Message-ID<4fe69696$0$16177$882e7ee2@usenet-news.net>
In reply to#20810
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.

[toc] | [prev] | [next] | [standalone]


#22661

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-06-25 02:41 -0700
Message-ID<61803198-a3b7-4cf7-a615-a9941d9393ba@googlegroups.com>
In reply to#20810
בתאריך יום שלישי, 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. 

[toc] | [prev] | [next] | [standalone]


#22692

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2012-06-25 16:29 +0000
Message-ID<jsa3la$2rs$2@news.xmission.com>
In reply to#22661
In article <61803198-a3b7-4cf7-a615-a9941d9393ba@googlegroups.com>,
Malcolm McLean  <malcolm.mclean5@btinternet.com> 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

[toc] | [prev] | [next] | [standalone]


#22806

Fromnick_keighley_nospam@hotmail.com
Date2012-06-26 08:27 -0700
Message-ID<24bb4002-32ec-4fe5-9971-7d9683ad5029@googlegroups.com>
In reply to#22692
On Monday, June 25, 2012 5:29:30 PM UTC+1, Kenny McCormack wrote:
> In article <61803198-a3b7-4cf7-a615-a9941d9393ba@googlegroups.com>,
> Malcolm McLean  <malcolm.mclean5@btinternet.com> 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). How about 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.

[toc] | [prev] | [next] | [standalone]


#22855

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2012-06-27 09:05 +0000
Message-ID<jseid8$a2o$1@news.xmission.com>
In reply to#22806
In article <24bb4002-32ec-4fe5-9971-7d9683ad5029@googlegroups.com>,
 <nick_keighley_nospam@hotmail.com> wrote:
>On Monday, June 25, 2012 5:29:30 PM UTC+1, Kenny McCormack wrote:
>> In article <61803198-a3b7-4cf7-a615-a9941d9393ba@googlegroups.com>,
>> Malcolm McLean  <malcolm.mclean5@btinternet.com> 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?

[toc] | [prev] | [next] | [standalone]


#22859

Fromnick_keighley_nospam@hotmail.com
Date2012-06-27 03:57 -0700
Message-ID<936e27e0-4ef3-480e-8cf5-99bb40b1a9a2@googlegroups.com>
In reply to#22855
On Wednesday, June 27, 2012 10:05:44 AM UTC+1, Kenny McCormack wrote:
> In article <24bb4002-32ec-4fe5-9971-7d9683ad5029@googlegroups.com>,
>  <nick_keighley_nospam@hotmail.com> wrote:
> >On Monday, June 25, 2012 5:29:30 PM UTC+1, Kenny McCormack wrote:
> >> In article <61803198-a3b7-4cf7-a615-a9941d9393ba@googlegroups.com>,
> >> Malcolm McLean  <malcolm.mclean5@btinternet.com> 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...

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web