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


Groups > comp.lang.c > #20868

Re: A question for this group...

From Stephen Sprunk <stephen@sprunk.org>
Newsgroups comp.lang.c
Subject Re: A question for this group...
Date 2012-05-16 18:18 -0500
Organization A noiseless patient Spider
Message-ID <jp1ck8$b9a$1@dont-email.me> (permalink)
References <jou4fd$5uh$1@news.xmission.com> <15497347.287.1337165607109.JavaMail.geo-discussion-forums@vbk4>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web