function explanation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jarno A Wuolijoki

    #16
    Re: function explanation

    On Fri, 26 Sep 2003, CBFalconer wrote:
    [color=blue][color=green][color=darkred]
    > > > What is difficult to understand about...?
    > > >
    > > > while (*s++ = *t++)
    > > > ;[/color]
    > >
    > > First, there's an off by one error. Second, it doesn't terminate
    > > the destination string.[/color]
    >
    > No there isn't (an error). Yes it does (terminate).[/color]

    And that's supposed to be easy to understand?


    *t++; /* "get()" */
    *s++=x; /* "put(x)" */
    *s++=*t++; /* "put(get()) " */

    while (tmp=*t++) *s++=tmp;
    /* "as long as we can get data, append it to s" */
    *s=0;
    /* terminate s; */

    while (*t) *s++=*t++;
    *s=0;
    /* "as long as there's data, copy it." */

    while (*s++=*t++) ;
    /* "as long as we have copied nonzero data, do nothing. perform extra
    copy as a side effect of embedding it in the test" */

    (of course if you interpret "*s++=*t++" as a routine which returns 0
    when it has finished it makes a whole lot more sense..)

    Comment

    • Irrwahn Grausewitz

      #17
      Re: function explanation

      Jarno A Wuolijoki <jwuolijo@cs.He lsinki.FI> wrote:
      [color=blue]
      >On Fri, 26 Sep 2003, CBFalconer wrote:
      >[color=green][color=darkred]
      >> > > What is difficult to understand about...?
      >> > >
      >> > > while (*s++ = *t++)
      >> > > ;
      >> >
      >> > First, there's an off by one error. Second, it doesn't terminate
      >> > the destination string.[/color]
      >>
      >> No there isn't (an error). Yes it does (terminate).[/color]
      >
      >And that's supposed to be easy to understand?[/color]

      Yes. :)

      <SNIP>

      Irrwahn
      --
      ERROR 103: Dead mouse in hard drive.

      Comment

      • pete

        #18
        Re: function explanation

        CBFalconer wrote:[color=blue]
        >
        > Jarno A Wuolijoki wrote:[color=green]
        > > On 25 Sep 2003, Peter Nilsson wrote:
        > >[color=darkred]
        > > > What is difficult to understand about...?
        > > >
        > > > while (*s++ = *t++)
        > > > ;[/color]
        > >
        > > First, there's an off by one error. Second, it doesn't terminate
        > > the destination string.[/color]
        >
        > No there isn't (an error). Yes it does (terminate). All assuming
        > the source is a legitimate string and source and destination do
        > not overlap.[/color]

        It depends on how they overlap.

        --
        pete

        Comment

        • Malcolm

          #19
          Re: function explanation


          "Jarno A Wuolijoki" <jwuolijo@cs.He lsinki.FI> wrote in message[color=blue]
          > On Fri, 26 Sep 2003, CBFalconer wrote:
          >[color=green][color=darkred]
          > > > > What is difficult to understand about...?
          > > > >
          > > > > while (*s++ = *t++)
          > > > > ;
          > > >
          > > > First, there's an off by one error. Second, it doesn't terminate
          > > > the destination string.[/color]
          > >
          > > No there isn't (an error). Yes it does (terminate).[/color]
          >
          > And that's supposed to be easy to understand?
          >[/color]
          Exactly. We have a construct that is supposedly "easy to understand",
          perfectly acceptable, idiomatic C, and first a newbie posts with "what does
          this do" and then another poster mistakes it for a bug.

          Most of C is basically the same as any other programming language. Someone
          who knows another language will easily understand

          int x = 0;

          he will also quickly get

          for(i=0;i<100;i ++)

          even though ++ is a pure C idiom.

          When it comes to pointers, he has some learning to do. He can quickly pick
          up that * is the indirection operator. A bit more difficult is that
          *ptr++ increments ptr, rather than *ptr, despite the fact that the ++ is
          applied last.

          However when we come to

          while(*s++ = *t++);

          it is a bridge too far for our experienced programmer who only knows a bit
          of C. Unless you know, it is deeply counter intutive that = in this context
          means "assign" rather than "compare". If he's lucky he will remember the
          pointer incrementing and dereference rules, but only just, and this will
          make him uncomfortable and unfamiliar with the whole expression. Finally,
          using a semi-colon to denote an empty loop is also something that has few
          parallels in other languages, and looks like a syntax error.

          In short, this expression will make the code harder to read and is likely to
          cause trouble. Even an experienced C programmer, though he will not
          misinterpret it, will probably take longer to read and understand the
          expression and verify that it is correct. This is particularly the case if
          it is embedded in a list of other expressions which are also
          counter-intutive and difficult to understand.


          Comment

          • pete

            #20
            Re: function explanation

            Malcolm wrote:
            [color=blue]
            > Even an experienced C programmer, though he will not
            > misinterpret it, will probably take longer to read and understand the
            > expression and verify that it is correct.[/color]

            "... the idiom should be mastered,
            because you will see it frequently in C programs."

            Mastering the idiom means, that you will recognize it,
            and know at a glance, what it means.

            If you have not mastered it, then it may be tough.
            But the mastery that particular idiom, is more common than not,
            among experienced C programmers.

            A few snippets for you:

            void copyarray(e_typ e *s1, e_type *s2, size_t nmemb)
            {
            while (nmemb--) {
            *s1++ = *s2++;
            }
            }

            char *squeeze(char *s1, const char *s2)
            {
            char const* p2;
            char *const p1 = s1;

            for (p2 = strtok(s1, s2); p2; p2 = strtok( 0, s2)) {
            while (*p2) {
            *s1++ = *p2++;
            }
            }

            *s1 = '\0';
            return p1;
            }

            static void merge(e_type *base, e_type *buffer, size_t nmemb)
            {
            if (nmemb > SMALL_MERGE) {
            size_t const half = nmemb / 2;
            e_type *const middle = base + half;
            e_type *const after_buffer = buffer + half;
            e_type *const after_array = base + nmemb;
            e_type *mid_ptr = middle;

            merge(base, buffer, half);
            merge(middle, buffer, nmemb - half);
            while (GTE(middle, base) && middle != base) {
            ++base;
            }
            buffer = after_buffer;
            while (base != mid_ptr) {
            *--buffer = *--mid_ptr;
            }
            mid_ptr = middle;
            while (middle != base) {
            *base++ = GT(buffer, mid_ptr) ? *mid_ptr++ : *buffer++;
            }
            while (after_buffer != buffer && after_array != mid_ptr) {
            *base++ = GT(buffer, mid_ptr) ? *mid_ptr++ : *buffer++;
            }
            while (after_buffer != buffer) {
            *base++ = *buffer++;
            }
            } else {
            si_sort(base, nmemb);
            }
            }

            --
            pete

            Comment

            • John Bode

              #21
              Re: function explanation

              Jarno A Wuolijoki <jwuolijo@cs.He lsinki.FI> wrote in message news:<Pine.LNX. 4.44.0309262217 080.7014-100000@melkinpa asi.cs.Helsinki .FI>...[color=blue]
              > On Fri, 26 Sep 2003, CBFalconer wrote:
              >[color=green][color=darkred]
              > > > > What is difficult to understand about...?
              > > > >
              > > > > while (*s++ = *t++)
              > > > > ;
              > > >
              > > > First, there's an off by one error. Second, it doesn't terminate
              > > > the destination string.[/color]
              > >
              > > No there isn't (an error). Yes it does (terminate).[/color]
              >
              > And that's supposed to be easy to understand?
              >[/color]

              Yes, after your brain has been sufficiently damaged^H^H^H^H ^H^H^H
              trained to recognize certain C idioms.

              Comment

              • Jarno A Wuolijoki

                #22
                Re: function explanation

                On Mon, 29 Sep 2003, pete wrote:
                [color=blue][color=green]
                > > Even an experienced C programmer, though he will not
                > > misinterpret it, will probably take longer to read and understand the
                > > expression and verify that it is correct.[/color]
                >
                > "... the idiom should be mastered,
                > because you will see it frequently in C programs."
                >
                > Mastering the idiom means, that you will recognize it,
                > and know at a glance, what it means.
                >
                > If you have not mastered it, then it may be tough.
                > But the mastery that particular idiom, is more common than not,
                > among experienced C programmers.
                >
                > A few snippets for you:[/color]

                Note that the "idiomatic strcpy" performed a transgression that
                none of your perfectly readable snippets did:

                while (loop_body) /*nothing*/ ;

                Sure I know what it really does, but then again, I know !strcmp(a, b)
                as well.

                Comment

                • Kevin Easton

                  #23
                  Re: function explanation

                  Jarno A Wuolijoki <jwuolijo@cs.he lsinki.fi> wrote:[color=blue]
                  > On Mon, 29 Sep 2003, pete wrote:
                  >[color=green][color=darkred]
                  >> > Even an experienced C programmer, though he will not
                  >> > misinterpret it, will probably take longer to read and understand the
                  >> > expression and verify that it is correct.[/color]
                  >>
                  >> "... the idiom should be mastered,
                  >> because you will see it frequently in C programs."
                  >>
                  >> Mastering the idiom means, that you will recognize it,
                  >> and know at a glance, what it means.
                  >>
                  >> If you have not mastered it, then it may be tough.
                  >> But the mastery that particular idiom, is more common than not,
                  >> among experienced C programmers.
                  >>
                  >> A few snippets for you:[/color]
                  >
                  > Note that the "idiomatic strcpy" performed a transgression that
                  > none of your perfectly readable snippets did:
                  >
                  > while (loop_body) /*nothing*/ ;[/color]

                  while (sideeffects) { }

                  might well be a clearer way of writing it. It's pretty hard to
                  misinterpret that.

                  - Kevin.

                  Comment

                  • CBFalconer

                    #24
                    Re: function explanation

                    Kevin Easton wrote:[color=blue]
                    >[/color]
                    .... snip ...[color=blue]
                    >
                    > while (sideeffects) { }
                    >
                    > might well be a clearer way of writing it. It's pretty hard to
                    > misinterpret that.[/color]

                    Which can be used to evolve a fairly efficient strlen:

                    while (*s++) continue;

                    Now all we have to know is how many time s was advanced, leading
                    to:

                    size_t strlen(char *s)
                    {
                    char *p = s;

                    while (*s++) continue;
                    return s - p - 1;
                    }

                    --
                    Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net> USE worldnet address!


                    Comment

                    Working...