About += and ++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Malcolm McLean

    #16
    Re: About += and ++


    "Stephen Sprunk" <stephen@sprunk .orgwrote in message
    Another major part of it is laziness. "++i" obviously takes less typing
    than "i=i+1", and takes less space to store in source form as well. When
    you get to more complicated idioms, like "while (*i++ = *j++);" vs "do {
    *i=*j; i=i+1; j=j+1; } while (*j);", the space savings gets significant,
    as does readability. Such things appear over and over in most programs,
    so it's normal that a language that "evolved" (as opposed to one that was
    "designed") would incorporate features to make the programmer's life
    easier for common tasks.
    >
    That terse strcpy() seems to have driven a lot of C grammar.

    However generally you write strcpy() once, if embedded, or never, on a
    hosted implementation.

    --
    Free games and programming goodies.


    Comment

    • Flash Gordon

      #17
      Re: About += and ++

      Malcolm McLean wrote, On 14/06/08 10:40:
      >
      "Stephen Sprunk" <stephen@sprunk .orgwrote in message
      >Another major part of it is laziness. "++i" obviously takes less
      >typing than "i=i+1", and takes less space to store in source form as
      >well. When you get to more complicated idioms, like "while (*i++ =
      >*j++);" vs "do { *i=*j; i=i+1; j=j+1; } while (*j);", the space
      >savings gets significant, as does readability. Such things appear
      >over and over in most programs, so it's normal that a language that
      >"evolved" (as opposed to one that was "designed") would incorporate
      >features to make the programmer's life easier for common tasks.
      >>
      That terse strcpy() seems to have driven a lot of C grammar.
      I suspect it was not done to implement strcpy, it is merely that strcpy
      is used as a common example.
      However generally you write strcpy() once, if embedded, or never, on a
      hosted implementation.
      Stephen never said that you implement strcpy more than one, he said that
      you get similar things over and over again. Similar is very different to
      identical.
      --
      Flash Gordon

      Comment

      • A. F. McClelland

        #18
        Re: About += and ++

        Willem <willem@stack.n lwrites:
        The compiler may or may not emit the same instructions
        for i += 1 versus i += 1,
        this has nothing to do with the ++i versus i += 1 issue.
        Did you mean i++ in one of those?

        Best,
        Tony

        Comment

        • Richard Tobin

          #19
          Re: About += and ++

          In article <86od64ur4g.fsf @tourifreet.wel lworld>,
          A. F. McClelland <t_mcclelland@b tinternet.comwr ote:
          >Willem <willem@stack.n lwrites:
          >The compiler may or may not emit the same instructions
          >for i += 1 versus i += 1,
          >this has nothing to do with the ++i versus i += 1 issue.
          >Did you mean i++ in one of those?
          I *think* he was pointing out that a compiler doesn't even have to
          generate the same code for two instances of the very same expression,
          so whether it generates the same code for two different expressions
          doesn't tell us much. But I could be wrong.

          -- Richard
          --
          In the selection of the two characters immediately succeeding the numeral 9,
          consideration shall be given to their replacement by the graphics 10 and 11 to
          facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

          Comment

          • Willem

            #20
            Re: About += and ++

            Richard Tobias wrote:
            ) In article <86od64ur4g.fsf @tourifreet.wel lworld>,
            ) A. F. McClelland <t_mcclelland@b tinternet.comwr ote:
            )
            )>Willem <willem@stack.n lwrites:
            )>The compiler may or may not emit the same instructions
            )>for i += 1 versus i += 1,
            )>this has nothing to do with the ++i versus i += 1 issue.
            )
            )>Did you mean i++ in one of those?
            )
            ) I *think* he was pointing out that a compiler doesn't even have to
            ) generate the same code for two instances of the very same expression,
            ) so whether it generates the same code for two different expressions
            ) doesn't tell us much. But I could be wrong.

            Spot on. I thought that was obvious from the (snipped) bit
            I was replying to.

            SaSW, Willem
            --
            Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
            #EOT

            Comment

            • Richard Tobin

              #21
              Re: About += and ++

              In article <slrng5837o.2l3 6.willem@snail. stack.nl>,
              Willem <willem@stack.n lwrote:
              >Richard Tobias wrote:
              Um, why did you change my name?

              -- Richard
              --
              In the selection of the two characters immediately succeeding the numeral 9,
              consideration shall be given to their replacement by the graphics 10 and 11 to
              facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

              Comment

              • Willem

                #22
                Re: About += and ++

                Richard Tobin wrote:
                ) In article <slrng5837o.2l3 6.willem@snail. stack.nl>,
                ) Willem <willem@stack.n lwrote:
                )>Richard Tobias wrote:
                )
                ) Um, why did you change my name?

                Oh sorry.. My posting software just displays the first name, but because
                there are so many Richards in this group I decided to add (what I thought)
                was your last name. I didn't mean anything by it, just a typo.


                SaSW, Willem
                --
                Disclaimer: I am in no way responsible for any of the statements
                made in the above text. For all I know I might be
                drugged or something..
                No I'm not paranoid. You all think I'm paranoid, don't you !
                #EOT

                Comment

                • CBFalconer

                  #23
                  Re: About += and ++

                  Willem wrote:
                  Richard Tobias wrote:
                  >A. F. McClelland <t_mcclelland@b tinternet.comwr ote:
                  >>Willem <willem@stack.n lwrites:
                  >>>
                  >>>The compiler may or may not emit the same instructions
                  >>>for i += 1 versus i += 1,
                  >>>this has nothing to do with the ++i versus i += 1 issue.
                  >>>
                  >>Did you mean i++ in one of those?
                  >>
                  >I *think* he was pointing out that a compiler doesn't even have
                  >to generate the same code for two instances of the very same
                  >expression, so whether it generates the same code for two
                  >different expressions doesn't tell us much. But I could be wrong.
                  >
                  Spot on. I thought that was obvious from the (snipped) bit
                  I was replying to.
                  Rather hard to connect it with snipped quotations. However, the
                  context of the statement obviously can affect the optimum code to
                  use.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.


                  ** Posted from http://www.teranews.com **

                  Comment

                  • David Thompson

                    #24
                    Re: About += and ++

                    On Fri, 13 Jun 2008 11:27:29 +0100, Chris Dollin <chris.dollin@h p.com>
                    wrote:
                    What is the compiler doing internally to increment the value
                    in that case?
                    Some machines (eg PDP-11) have fancy "increment" instructions
                    which will add 1 to a register (or even a memory location).
                    I wouldn't call that 'fancy'. Maybe 'specialized'. Another example
                    probably(!) more familiar to people today is x86.
                    Other machines have to use their ordinary "add" instruction with
                    an operand of literal-one (eg ARM, in regsisters). Yet other
                    Or (slightly different) an add-immed insn with operand 1.
                    machines might need to use an add-register instruction after
                    loading one of the registers with the value 1.
                    >
                    Some also can add a small integer by (ab?)using effective-address
                    calculation, sometimes faster than an explicit ADD or even INC.

                    None of which alters your point that this is implementation (and
                    mostly target) dependent.

                    - formerly david.thompson1 || achar(64) || worldnet.att.ne t

                    Comment

                    • John Bode

                      #25
                      Re: About += and ++

                      On Jun 13, 5:38 am, Antoninus Twink <nos...@nospam. invalidwrote:
                      On 13 Jun 2008 at 10:27, santosh wrote:
                      >
                      But the compiler may or may not emit the same instructions for i += 1
                      versus ++i.
                      >
                      Can you name a compiler that doesn't?
                      Can you state with absolute certainty that all C compilers everywhere
                      do? In all possible situations?

                      Comment

                      • Richard

                        #26
                        Re: About += and ++

                        John Bode <jfbode1029@gma il.comwrites:
                        On Jun 13, 5:38 am, Antoninus Twink <nos...@nospam. invalidwrote:
                        >On 13 Jun 2008 at 10:27, santosh wrote:
                        >>
                        But the compiler may or may not emit the same instructions for i += 1
                        versus ++i.
                        >>
                        >Can you name a compiler that doesn't?
                        >
                        Can you state with absolute certainty that all C compilers everywhere
                        do? In all possible situations?
                        Of course not, in the same way I can not be assured that the standard
                        means the same thing to some language lawyers in another state. But
                        meanwhile in the real world where common sense applies ....

                        Comment

                        • John Bode

                          #27
                          Re: About += and ++

                          On Jun 24, 10:31 am, Richard<rgr...@ gmail.comwrote:
                          John Bode <jfbode1...@gma il.comwrites:
                          On Jun 13, 5:38 am, Antoninus Twink <nos...@nospam. invalidwrote:
                          On 13 Jun 2008 at 10:27, santosh wrote:
                          >
                          But the compiler may or may not emit the same instructions for i += 1
                          versus ++i.
                          >
                          Can you name a compiler that doesn't?
                          >
                          Can you state with absolute certainty that all C compilers everywhere
                          do? In all possible situations?
                          >
                          Of course not, in the same way I can not be assured that the standard
                          means the same thing to some language lawyers in another state. But
                          meanwhile in the real world where common sense applies ....
                          In the "real world" where common sense applies, it's reasonable to
                          point out that not all compilers are guaranteed to behave the same way
                          for a given language construct (*especially* for the pre/post
                          increment operators). Normally that would be an uncontroversial
                          statement, but you and Twink are so eager to pounce on any perceived
                          instance of pedantry that you come off sounding like idiots.

                          Comment

                          • Harald van =?UTF-8?b?RMSzaw==?=

                            #28
                            Re: About += and ++

                            On Fri, 13 Jun 2008 10:38:41 +0000, Antoninus Twink wrote:
                            On 13 Jun 2008 at 10:27, santosh wrote:
                            >But the compiler may or may not emit the same instructions for i += 1
                            >versus ++i.
                            >
                            Can you name a compiler that doesn't?
                            Yes. Nils Weller's compiler (from <http://nwcc.sourceforg e.net/>) doesn't
                            optimise at all, and generates different code for the two expressions in
                            the version I tried.

                            Comment

                            Working...