Highly efficient string reversal code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dominantubergeek@ymail.com

    Highly efficient string reversal code

    Hello, I'm a highly experienced expert C programmer and I've written
    this code to reverse a string in place. I think you could all learn
    something from it!

    int reverse(char* reverseme){
    int retval=-1;
    if(retval!=NULL ){
    int len=strlen(retv al){
    if(len>0){
    int half=len>>1;
    for(;retval<hal f;retval++){
    reverseme[retval]^=reverseme[len-(retval+1)];
    reverseme[len-(retval+1)]=reverseme[retval];
    reverseme[retval]^=reverseme[len-(retval+1)];
    }
    }
    }
    return retval;
    }

    -Phil/CERisE
  • Martin Ambuhl

    #2
    Re: Highly efficient string reversal code

    dominantubergee k@ymail.com wrote:
    Hello, I'm a highly experienced expert C programmer
    No, you are not.
    and I've written
    this code to reverse a string in place. I think you could all learn
    something from it!
    I doubt it. You should rethink it. It is not, as you subject line
    claims "highly efficient".

    Comment

    • Lew Pitcher

      #3
      Re: Highly efficient string reversal code

      On September 22, 2008 14:42, in comp.lang.c, dominantubergee k@ymail.com
      (dominantuberge ek@ymail.com) wrote:
      Hello, I'm a highly experienced expert C programmer
      Apparently, you are
      a) egotistical, and
      b) wrong
      and I've written
      this code to reverse a string in place.
      I am /so/ sorry, both for you and your employer.
      I think you could all learn
      something from it!
      I'm sure that you are right. After all, /I/ learned that you are much less
      than the C programmer that you claim to be.
      int reverse(char* reverseme){
      int retval=-1;
      if(retval!=NULL )
      Useless test, as retval was initialized (and never altered) to a value
      that will not equate to NULL
      {
      int len=strlen(retv al)
      First real failure. retval is an integer, and the argument to
      strlen() is a pointer-to-char. You can't get a valid result from
      taking the "string length" of an integer.

      Second failure: you did not #include the header that declares the
      strlen() function. Thus, your compiler did not flag the above
      assignment as an error (argument of incompatable type)
      {
      if(len>0)
      So, what is the "string length" of an integer? And when would it be
      greater than zero?
      {
      int half=len>>1;
      Style hack: To be clearer to the intent of what sort of number
      half represents, you should have coded this as
      int half = len / 2;
      Of course, since len is a nonsense value, half will also be a
      nonsense value.

      for(;retval<hal f;retval++){
      reverseme[retval]^=reverseme[len-(retval+1)];
      Undefined behaviour on first iteration, when retval == -1
      reverseme[-1] is out of bounds.
      reverseme[len-(retval+1)]=reverseme[retval];
      Undefined behaviour on first iteration - same reason

      reverseme[retval]^=reverseme[len-(retval+1)];
      Undefined behaviour on first iteration - same reason

      Plus, you've screwed up the "xor trick", and wiped out /both/
      ends of the string with invalid values.

      The head end now contains an array of 0x00 (up to
      the ill-computed "midpoint") , while the tail end contains the
      results of the head end xor the tail end.
      }
      }
      }
      return retval;
      Of what use is this return value?
      }
      >
      -Phil/CERisE
      --
      Lew Pitcher

      Master Codewright & JOAT-in-training | Registered Linux User #112576
      http://pitcher.digitalfreehold.ca/ | GPG public key available by request
      ---------- Slackware - Because I know what I'm doing. ------


      Comment

      • Eric Sosman

        #4
        Re: Highly efficient string reversal code

        dominantubergee k@ymail.com wrote:
        Hello, I'm a highly experienced expert C programmer [...]
        s/C programmer/PLONKed troll/

        --
        Eric.Sosman@sun .com

        Comment

        • Default User

          #5
          Re: Highly efficient string reversal code

          dominantubergee k@ymail.com wrote:
          Hello, I'm a highly experienced expert C programmer and I've written
          this code to reverse a string in place. I think you could all learn
          something from it!
          You should learn some better trolling techniques. I suggest studying
          under Bill Cunningham.




          Brian

          Comment

          • Flash Gordon

            #6
            Re: Highly efficient string reversal code

            Lew Pitcher wrote, On 22/09/08 20:06:
            On September 22, 2008 14:42, in comp.lang.c, dominantubergee k@ymail.com
            (dominantuberge ek@ymail.com) wrote:
            >
            >Hello, I'm a highly experienced expert C programmer
            >
            Apparently, you are
            a) egotistical, and
            b) wrong
            Agreed. Although you missed some of the problems...
            >and I've written
            >this code to reverse a string in place.
            >
            I am /so/ sorry, both for you and your employer.
            You think s/he is employed?
            >I think you could all learn
            >something from it!
            >
            I'm sure that you are right. After all, /I/ learned that you are much less
            than the C programmer that you claim to be.
            >
            >int reverse(char* reverseme){
            > int retval=-1;
            > if(retval!=NULL )
            >
            Useless test, as retval was initialized (and never altered) to a value
            that will not equate to NULL
            <snip load more broken code>

            You forgot to mention that as retval is an int it won't actually compile
            on all implementations .

            I think it was a troll. However, it was amusing for how bad the code
            was. I wonder how many other errors it has that have been missed?
            --
            Flash Gordon
            If spamming me sent it to smap@spam.cause way.com
            If emailing me use my reply-to address
            See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

            Comment

            • Keith Thompson

              #7
              Re: Highly efficient string reversal code

              Lew Pitcher <lpitcher@teksa vvy.comwrites:
              On September 22, 2008 14:42, in comp.lang.c, dominantubergee k@ymail.com
              (dominantuberge ek@ymail.com) wrote:
              [...]
              > int retval=-1;
              > if(retval!=NULL )
              >
              Useless test, as retval was initialized (and never altered) to a value
              that will not equate to NULL
              [...]

              It's not just useless. retval is an int; NULL expands to a null
              pointer constant. If NULL happens to be #defined as 0, the test is
              merely useless; if it's #defined as ((void*)0) or something similar,
              the test is a constraint violation.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Keith Thompson

                #8
                Re: Highly efficient string reversal code

                Flash Gordon <smap@spam.caus eway.comwrites:
                [...]
                I think it was a troll. However, it was amusing for how bad the code
                was. I wonder how many other errors it has that have been missed?
                I don't think anybody has mentioned the syntax error yet.
                int len=strlen(retv al){
                should be
                int len=strlen(retv al);

                I'd call it a comedy of errors, but that would require it to be funny.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • dj3vande@csclub.uwaterloo.ca.invalid

                  #9
                  Re: Highly efficient string reversal code

                  In article <dc4b6$48d7eb24 $4c0a8845$4800@ TEKSAVVY.COM-Free>,
                  Lew Pitcher <lpitcher@teksa vvy.comwrote:
                  >On September 22, 2008 14:42, in comp.lang.c, dominantubergee k@ymail.com
                  >(dominantuberg eek@ymail.com) wrote:
                  >
                  >Hello, I'm a highly experienced expert C programmer
                  >
                  >Apparently, you are
                  >a) egotistical, and
                  >b) wrong
                  >
                  >and I've written
                  >this code to reverse a string in place.
                  >
                  >I am /so/ sorry, both for you and your employer.
                  >
                  >I think you could all learn
                  >something from it!
                  >
                  >I'm sure that you are right. After all, /I/ learned that you are much less
                  >than the C programmer that you claim to be.
                  Since the subject has come up, here's one I've been waiting for an
                  excuse to post:
                  --------
                  typedef struct l{char a;struct l*b;}l;
                  static void x(char*a,l*b) {if(b){(-1)[a]=b->a;x(--a,b->b);}}
                  static void y(char*a,l*b,l* c) {if(b){l*d=b->b;b->b=c;y(a,d,b);} else{x(a,c);}}
                  static void z(char*a,l*b) {if(*a){l c;c.a=*a;c.b=b; z(a+1,&c);}else {y(a,b,0);}}
                  void reverse(char *s) { z(s,0); }
                  --------
                  Unlike the one that started the thread, this has actually been tested
                  and verified to work.

                  It also uses advanced implementation techniques that allow programs
                  with certain properties (especially in languages with properties that
                  make those program properties easy to detect and exploit) to be
                  compiled to highly efficient code.

                  Somebody who spends enough time untangling it might even learn
                  something useful from it (for sufficiently, but not comically, loose
                  values of "useful").


                  dave
                  (bonus points if you can figure out where the idea to do it that way came from)

                  --
                  Dave Vandervies dj3vande at eskimo dot com

                  None of my copies of K&R have a "screen". Should I apply for a refund?
                  --Chris Dollin in comp.lang.c

                  Comment

                  • Jens Stueckelberger

                    #10
                    Re: Highly efficient string reversal code

                    On Mon, 22 Sep 2008 11:42:37 -0700, dominantubergee k wrote:
                    Hello, I'm a highly experienced expert C programmer and I've written
                    this code to reverse a string in place. I think you could all learn
                    something from it!
                    Boy, that last sure is going to endear you to people here!

                    Comment

                    • Richard

                      #11
                      Re: Highly efficient string reversal code

                      Jens Stueckelberger <JStueckelberge r@nowhere.orgwr ites:
                      On Mon, 22 Sep 2008 11:42:37 -0700, dominantubergee k wrote:
                      >
                      >Hello, I'm a highly experienced expert C programmer and I've written
                      >this code to reverse a string in place. I think you could all learn
                      >something from it!
                      >
                      Boy, that last sure is going to endear you to people here!
                      >
                      Suggesting people show new programmers pointers at work in a debugger
                      endears you to some of the dustier c.l.c dweller ... :-;

                      Comment

                      • Bartc

                        #12
                        Re: Highly efficient string reversal code


                        <dj3vande@csclu b.uwaterloo.ca. invalidwrote in message
                        news:gb8vs6$amh $1@rumours.uwat erloo.ca...
                        Since the subject has come up, here's one I've been waiting for an
                        excuse to post:
                        --------
                        typedef struct l{char a;struct l*b;}l;
                        static void x(char*a,l*b) {if(b){(-1)[a]=b->a;x(--a,b->b);}}
                        static void y(char*a,l*b,l* c)
                        {if(b){l*d=b->b;b->b=c;y(a,d,b);} else{x(a,c);}}
                        static void z(char*a,l*b) {if(*a){l
                        c;c.a=*a;c.b=b; z(a+1,&c);}else {y(a,b,0);}}
                        void reverse(char *s) { z(s,0); }
                        --------
                        Unlike the one that started the thread, this has actually been tested
                        and verified to work.
                        >
                        It also uses advanced implementation techniques that allow programs
                        with certain properties (especially in languages with properties that
                        make those program properties easy to detect and exploit) to be
                        compiled to highly efficient code.
                        >
                        Somebody who spends enough time untangling it might even learn
                        something useful from it (for sufficiently, but not comically, loose
                        values of "useful").
                        I tested your reverse function against the simplest reverse function of my
                        own I could knock up. Mine was 6 to 20 times faster that yours, to reverse a
                        77-character string one million times on my machine.

                        So if there is a point to your version, I haven't quite grasped it.

                        void myreverse(char *s) {
                        int i,j;
                        char c;

                        if (s==NULL) return;

                        j=strlen(s)-1;
                        i=0;

                        while (i<j) {
                        c=s[i];
                        s[i]=s[j];
                        s[j]=c;
                        ++i;
                        --j;
                        }

                        }

                        --
                        Bartc

                        Comment

                        • CBFalconer

                          #13
                          Re: Highly efficient string reversal code

                          dominantubergee k@ymail.com wrote:
                          >
                          Hello, I'm a highly experienced expert C programmer and I've
                          written this code to reverse a string in place. I think you could
                          all learn something from it!
                          >
                          int reverse(char* reverseme){
                          int retval=-1;
                          if(retval!=NULL ){
                          int len=strlen(retv al){
                          if(len>0){
                          int half=len>>1;
                          for(;retval<hal f;retval++){
                          reverseme[retval]^=reverseme[len-(retval+1)];
                          reverseme[len-(retval+1)]=reverseme[retval];
                          reverseme[retval]^=reverseme[len-(retval+1)];
                          }
                          }
                          }
                          return retval;
                          }
                          This is so horrible and faulty that I have to attach the
                          following. Don't forget to #include <strings.h>. Note that it
                          returns strlen.

                          /* =============== ======== */
                          /* reverse string in place */
                          size_t revstring(char *stg)
                          {
                          char *last, temp;
                          size_t lgh;

                          lgh = strlen(stg);
                          if (lgh 1) {
                          last = stg + lgh; /* points to '\0' */
                          while (last-- stg) {
                          temp = *stg; *stg++ = *last; *last = temp;
                          }
                          }
                          return lgh;
                          } /* revstring */

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

                          Comment

                          • dj3vande@csclub.uwaterloo.ca.invalid

                            #14
                            Re: Highly efficient string reversal code

                            In article <9DUBk.61567$E4 1.7240@text.new s.virginmedia.c om>,
                            Bartc <bc@freeuk.comw rote:
                            >
                            ><dj3vande@cscl ub.uwaterloo.ca .invalidwrote in message
                            >news:gb8vs6$am h$1@rumours.uwa terloo.ca...
                            >
                            >Since the subject has come up, here's one I've been waiting for an
                            >excuse to post:
                            [...]
                            >I tested your reverse function against the simplest reverse function of my
                            >own I could knock up. Mine was 6 to 20 times faster that yours, to reverse a
                            >77-character string one million times on my machine.
                            >
                            >So if there is a point to your version, I haven't quite grasped it.
                            I'd rather give my version to somebody who's posting an obvious
                            homework problem than yours.

                            Once you untangle it, it also demonstrates a few code-structure ideas
                            that are quite useful to have wrapped your brain around (not that
                            reversing a string in C is really a _good_ way to illustrate those),
                            and I like to think it provides a nifty example of how to build working
                            approximations to some high-level abstractions that C doesn't provide
                            natively.


                            dave
                            (specifically NOT claiming that "nifty" implies "useful in production code".)

                            --
                            Dave Vandervies dj3vande at eskimo dot com

                            None of my copies of K&R have a "screen". Should I apply for a refund?
                            --Chris Dollin in comp.lang.c

                            Comment

                            • Richard

                              #15
                              Re: Highly efficient string reversal code

                              CBFalconer <cbfalconer@yah oo.comwrites:
                              dominantubergee k@ymail.com wrote:
                              >>
                              >Hello, I'm a highly experienced expert C programmer and I've
                              >written this code to reverse a string in place. I think you could
                              >all learn something from it!
                              >>
                              >int reverse(char* reverseme){
                              > int retval=-1;
                              > if(retval!=NULL ){
                              > int len=strlen(retv al){
                              > if(len>0){
                              > int half=len>>1;
                              > for(;retval<hal f;retval++){
                              > reverseme[retval]^=reverseme[len-(retval+1)];
                              > reverseme[len-(retval+1)]=reverseme[retval];
                              > reverseme[retval]^=reverseme[len-(retval+1)];
                              > }
                              > }
                              > }
                              > return retval;
                              >}
                              >
                              This is so horrible and faulty that I have to attach the
                              following. Don't forget to #include <strings.h>. Note that it
                              returns strlen.
                              >
                              /* =============== ======== */
                              /* reverse string in place */
                              size_t revstring(char *stg)
                              {
                              char *last, temp;
                              size_t lgh;
                              >
                              lgh = strlen(stg);
                              if (lgh 1) {
                              last = stg + lgh; /* points to '\0' */
                              while (last-- stg) {
                              temp = *stg; *stg++ = *last; *last = temp;
                              }
                              }
                              return lgh;
                              } /* revstring */
                              You know I really had to look hard at this to understand it. Horrible
                              variables names almost purposely made "no standard" as far as C around
                              the world goes for such a function. Unnecessary length
                              check. Unnecessary size_t. Multiple assignments on one line
                              making perusal with a debugger almost impossible.

                              Much nicer IMO:

                              #include <string.h>

                              char * my_strrev(char *s){
                              char t;
                              char *e = s+strlen(s)-1;
                              while(e>s){
                              t = *s;
                              *s++ = *e;
                              *e-- = t;
                              }
                              }

                              I'm sure it breaks some sort of ISO magic, but it seems more concise and
                              readable to me. No I didnt test it, but I guess the idea should be self
                              evident.


                              Comment

                              Working...