String reversing problem

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

    #16
    Re: String reversing problem


    Keith Thompson wrote:[color=blue]
    > "tmp123" <tmp123@menta.n et> writes:
    > [...][color=green]
    > > void revstring ( char *s )
    > > {
    > > char *e;
    > > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=* e);
    > > }[/color]
    >
    > Undefined behavior.
    >[/color]

    Could be... but, could you prove your statement?

    Comment

    • Tim Rentsch

      #17
      Re: String reversing problem

      "tmp123" <tmp123@menta.n et> writes:
      [color=blue]
      > Keith Thompson wrote:[color=green]
      > > "tmp123" <tmp123@menta.n et> writes:
      > > [...][color=darkred]
      > > > void revstring ( char *s )
      > > > {
      > > > char *e;
      > > > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=* e);
      > > > }[/color]
      > >
      > > Undefined behavior.
      > >[/color]
      >
      > Could be... but, could you prove your statement?[/color]

      Even the first half of the control expression (namely, 's!=e--')
      can yield undefined behavior if strlen(s) == 0.

      Comment

      • Keith Thompson

        #18
        Re: String reversing problem

        "tmp123" <tmp123@menta.n et> writes:[color=blue]
        > Keith Thompson wrote:[color=green]
        >> "tmp123" <tmp123@menta.n et> writes:
        >> [...][color=darkred]
        >> > void revstring ( char *s )
        >> > {
        >> > char *e;
        >> > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=* e);
        >> > }[/color]
        >>
        >> Undefined behavior.
        >>[/color]
        >
        > Could be... but, could you prove your statement?[/color]

        The expression
        *s^=*e^=*s++^=* e
        modifies both *s and *e twice between sequence points.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Christian Bau

          #19
          Re: String reversing problem

          In article <1135977804.856 478.196090@g47g 2000cwa.googleg roups.com>,
          "tmp123" <tmp123@menta.n et> wrote:
          [color=blue]
          > Keith Thompson wrote:[color=green]
          > > "tmp123" <tmp123@menta.n et> writes:
          > > [...][color=darkred]
          > > > void revstring ( char *s )
          > > > {
          > > > char *e;
          > > > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=* e);
          > > > }[/color]
          > >
          > > Undefined behavior.
          > >[/color]
          >
          > Could be... but, could you prove your statement?[/color]

          It's kind of obvious. Even if it wasn't, whoever wrote that kind of code
          should be slapped silly. Immediate removal from any programming team
          that I am involved with.

          Comment

          • Chuck F.

            #20
            Re: String reversing problem

            tmp123 wrote:[color=blue]
            > Chuck F. wrote:[color=green]
            >> tmp123 wrote:[color=darkred]
            >> > int revstring ( char *s )
            >> > {
            >> > char *e;
            >> > int r;
            >> > for( e=s+(r=strlen(s ))-1; s<e; *s^=*e^=*s^=*e, s++, e--);
            >> > return r;
            >> > }[/color]
            >>
            >> FYI your version invokes undefined behaviour. Try it with a
            >> string of zero chars. My length test is not there for fun. I
            >> also have evil suspicions about the xor operations.[/color]
            >
            > If length is 0, then e=s-1, thus s<e is false exiting loop.
            >
            > However, if the usage of pointer comparation and pointer
            > substraction is not welcome, another version:[/color]
            [color=blue]
            > void revstring ( char *s )
            > {
            > char *e;
            > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=* e);
            > }[/color]

            Same problem and undefined behaviour. You are not allowed to
            generate a pointer that points before s (although you are allowed
            to generate one that points just after s).

            --
            "If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers." - Keith Thompson
            More details at: <http://cfaj.freeshell. org/google/>

            Comment

            • tmp123

              #21
              Re: String reversing problem

              To Mr. Keith Thompson and Mr. Tim Rentsch:

              Thanks for your replies, always open to learn something new.
              The first comment mades me doubt: if strlen(s)==0, s==e, thus s!=e-- is
              false and exit loop.
              The second comments, about sequence points, well, I must recognize I do
              not know what do you refer as "sequence points". An explanation or a
              reference will be welcome.

              To Mr. Christian Bau:

              Your English seems not to be the most valid to be used in net, because
              could be easily confused with agressive, specially by non-English
              people. Moreover, it could be taken as a confusion between what is a
              medium to interchange knowledgment, and what is a real programming
              team.

              Kind regards.

              Comment

              • Tim Rentsch

                #22
                Re: String reversing problem

                "tmp123" <tmp123@menta.n et> writes:
                [color=blue]
                > To Mr. Keith Thompson and Mr. Tim Rentsch:
                >
                > Thanks for your replies, always open to learn something new.
                > The first comment mades me doubt: if strlen(s)==0, s==e, thus s!=e-- is
                > false and exit loop.[/color]

                The problem is that the variable 'e' is decremented even if it is
                equal to the address held in 's'. That means the decrement can
                attempt to set 'e' to an address "before" the first element of
                the array object holding the string, which is disallowed. It's
                allowed to point to one element past the end of an array object,
                but not allowed to point to one element before the beginning.

                Comment

                • Christian Bau

                  #23
                  Re: String reversing problem

                  In article <1135980646.561 628.174380@g49g 2000cwa.googleg roups.com>,
                  "tmp123" <tmp123@menta.n et> wrote:
                  [color=blue]
                  > To Mr. Keith Thompson and Mr. Tim Rentsch:
                  >
                  > Thanks for your replies, always open to learn something new.
                  > The first comment mades me doubt: if strlen(s)==0, s==e, thus s!=e-- is
                  > false and exit loop.
                  > The second comments, about sequence points, well, I must recognize I do
                  > not know what do you refer as "sequence points". An explanation or a
                  > reference will be welcome.
                  >
                  > To Mr. Christian Bau:
                  >
                  > Your English seems not to be the most valid to be used in net, because
                  > could be easily confused with agressive, specially by non-English
                  > people. Moreover, it could be taken as a confusion between what is a
                  > medium to interchange knowledgment, and what is a real programming
                  > team.[/color]

                  Nothing wrong with my english. Lots wrong with your code. I don't care
                  too much about the undefined behavior, because you managed to write
                  completely incomprehensibl e code for a very simple task.

                  void reverse_string (char* s)
                  {
                  int i = 0;
                  int j = strlen (s) - 1;

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

                  works and is easy to understand.

                  Comment

                  • tmp123

                    #24
                    Re: String reversing problem

                    Thanks for the explanation.

                    I do not know any compiler nor OS with problems for this pointer (it is
                    only assigned, not used, and probably the final address will be valid).


                    However, if you say that standard allows to pass the end of the array
                    but not point before, I can accept the reasoning.

                    Kind regards.

                    Comment

                    • Mark McIntyre

                      #25
                      Re: String reversing problem

                      On 30 Dec 2005 14:10:46 -0800, in comp.lang.c , "tmp123"
                      <tmp123@menta.n et> wrote:
                      [color=blue]
                      > I do
                      >not know what do you refer as "sequence points". An explanation or a
                      >reference will be welcome.[/color]

                      ©ISO/IEC ISO/IEC 9899:1999 (E)
                      Annex C
                      (informative)
                      Sequence points
                      1 The following are the sequence points described in 5.1.2.3:
                      — The call to a function, after the arguments have been evaluated
                      (6.5.2.2).
                      — The end of the first operand of the following operators: logical AND
                      && (6.5.13);
                      logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17).
                      — The end of a full declarator: declarators (6.7.5);
                      — The end of a full expression: an initializer (6.7.8); the expression
                      in an expression
                      statement (6.8.3); the controlling expression of a selection statement
                      (if or switch)
                      (6.8.4); the controlling expression of a while or do statement
                      (6.8.5); each of the
                      expressions of a for statement (6.8.5.3); the expression in a return
                      statement
                      (6.8.6.4).
                      — Immediately before a library function returns (7.1.4).
                      — After the actions associated with each formatted input/output
                      function conversion
                      specifier (7.19.6, 7.24.2).
                      — Immediately before and immediately after each call to a comparison
                      function, and
                      also between any call to a comparison function and any movement of the
                      objects
                      passed as arguments to that call (7.20.5).

                      (Of Christian's comment)[color=blue]
                      >Your English seems not to be the most valid to be used in net,[/color]

                      Grow a thicker skin.

                      Personally I also thought the code was an abhomination and had I
                      discovered it in a project I was running I'd have told the programmer
                      to remove it forthwith.

                      Mark McIntyre
                      --

                      ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                      Comment

                      • Mark McIntyre

                        #26
                        Re: String reversing problem

                        On 30 Dec 2005 15:04:08 -0800, in comp.lang.c , "tmp123"
                        <tmp123@menta.n et> wrote:
                        [color=blue]
                        >Thanks for the explanation.
                        >
                        >I do not know any compiler nor OS with problems for this pointer[/color]

                        Thats not relevant. If it breaks the rules of the C standard, then its
                        not guaranteed to work, and you should not do it. One day your code
                        will be run on an OS which does care, and you will be fired / lose
                        money / lose face or whatever because of your code error.

                        Mark McIntyre
                        --

                        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                        Comment

                        • Tim Rentsch

                          #27
                          Re: String reversing problem

                          "tmp123" <tmp123@menta.n et> writes:
                          [color=blue]
                          > Thanks for the explanation.[/color]

                          You're welcome, glad it was of help.

                          [color=blue]
                          > I do not know any compiler nor OS with problems for this pointer (it is
                          > only assigned, not used, and probably the final address will be valid).[/color]

                          In most practical cases it won't be a problem. However, the Standard
                          is quite unambiguous that it is potentially a problem, and there are
                          some implementations (I'm pretty sure) where it fails.

                          [color=blue]
                          > However, if you say that standard allows to pass the end of the array
                          > but not point before, I can accept the reasoning.[/color]

                          One way to learn about these things is to get a copy of the
                          Standard, and read it yourself. There's an updated version
                          you can get just by downloading:



                          If you get a copy then you can read up on sequence points or
                          whatever else (including array indexing and pointer arithmetic)
                          and many of the comments on comp.lang.c will make a lot more
                          sense.

                          [color=blue]
                          > Kind regards.[/color]

                          Likewise. And Happy New Year.

                          Comment

                          • Richard Heathfield

                            #28
                            Re: String reversing problem

                            tmp123 said:
                            [color=blue]
                            > Thanks for the explanation.
                            >
                            > I do not know any compiler nor OS with problems for this pointer[/color]

                            The backroom boys are working on one right now, for all you know. And it
                            might just be tomorrow's sensational new toy for other, unrelated reasons.
                            And your boss might just say, "let's migrate all our code to this new
                            thing", as bosses often do. And at that point, the guys who stuck to the
                            rules will have working code, and the guys who didn't, won't.

                            So it pays to do things properly.

                            --
                            Richard Heathfield
                            "Usenet is a strange place" - dmr 29/7/1999

                            email: rjh at above domain (but drop the www, obviously)

                            Comment

                            • tmp123

                              #29
                              Re: String reversing problem

                              Hi,

                              See inlines:

                              Christian Bau wrote:[color=blue]
                              > tmp123 wrote:[color=green]
                              > > To Mr. Christian Bau:
                              > >
                              > > Your English seems not to be the most valid to be used in net, because
                              > > could be easily confused with agressive, specially by non-English
                              > > people. Moreover, it could be taken as a confusion between what is a
                              > > medium to interchange knowledgment, and what is a real programming
                              > > team.[/color]
                              >
                              > Nothing wrong with my english. Lots wrong with your code. I don't care
                              > too much about the undefined behavior, because you managed to write
                              > completely incomprehensibl e code for a very simple task.[/color]

                              Code that has been clearly state as "just for fun", that is, as an
                              academic experiment. At least, me, I get new knoledgment about pointer
                              before arrays and sequence points. Thanks to persons who have provided
                              it.

                              And taken into account that this piece of code has been presented as
                              "the good one" in a real programming team, some comments about:
                              [color=blue]
                              >
                              > void reverse_string (char* s)
                              > {
                              > int i = 0;
                              > int j = strlen (s) - 1;[/color]

                              1) It is not the same initialize a variable as give a variable the
                              first value it will take.
                              2) Not always a stack to add variables is available, or to add more
                              variables to it. Sometimes only modify code is allowed (i.e: patching
                              firmware in real time systems without stop them).
                              3) Relation between names "i" and "j" and their meaning/usage is
                              totally lost.
                              [color=blue]
                              >
                              > while (i < j)[/color]

                              4) Never heard about "for" statement?. It is used to enclose in an easy
                              to read statement all control of the loop iterators (initialization s,
                              exit condition and state update).
                              [color=blue]
                              > {
                              > char tmp = s [i];[/color]

                              5) Declare char here, far of the semantically parent of it (char *s) it
                              is only a way to hide things. And lots of compilers will ignore it (no
                              new frame).
                              [color=blue]
                              > s [i] = s [j];
                              > s [j] = tmp;[/color]

                              6) It seems this code must always be compiled with latest version of
                              advanced compilers. The responsability to convert array index
                              calculations to pointer operations, even integers to pointers, is
                              transferred to compiler.
                              [color=blue]
                              > ++i;
                              > --j;[/color]

                              7) Lost lines here?
                              [color=blue]
                              > }
                              > }
                              >
                              > works and is easy to understand.[/color]

                              8) "Works" is not a measure of quality.

                              This is my last post in this subject. I don not like to be troll, nor
                              feed trolls.

                              Kind regards.

                              Comment

                              • slebetman@yahoo.com

                                #30
                                Re: String reversing problem

                                tmp123 wrote:[color=blue]
                                > Hi,
                                >
                                > See inlines:
                                >
                                > Christian Bau wrote:[color=green]
                                > > tmp123 wrote:[color=darkred]
                                > > > To Mr. Christian Bau:
                                > > >
                                > > > Your English seems not to be the most valid to be used in net, because
                                > > > could be easily confused with agressive, specially by non-English
                                > > > people. Moreover, it could be taken as a confusion between what is a
                                > > > medium to interchange knowledgment, and what is a real programming
                                > > > team.[/color]
                                > >
                                > > Nothing wrong with my english. Lots wrong with your code. I don't care
                                > > too much about the undefined behavior, because you managed to write
                                > > completely incomprehensibl e code for a very simple task.[/color]
                                >
                                > Code that has been clearly state as "just for fun", that is, as an
                                > academic experiment. At least, me, I get new knoledgment about pointer
                                > before arrays and sequence points. Thanks to persons who have provided
                                > it.[/color]

                                "For fun" still doesn't make your code correct. You stated that it
                                works but the experts here have pointed out that even so it is still
                                not correct "C". So that doesn't invalidate Mark's comments - your code
                                still is horrible, learn and move on.

                                As for your comment about Mark's tone being aggressive, you have to
                                learn that this is Usenet, and taking on an aggressive tone is a
                                tradition of the net long before you showed up. Some groups liks
                                comp.lang.tcl may be less aggressive but comp.lang.c is aggressive (I
                                learned that the hard way). Maybe it's because so many people keep
                                asking the same stupid questions here over and over again (and keep
                                making the same stupid mistakes that others have made before).
                                [color=blue][color=green]
                                > > void reverse_string (char* s)
                                > > {
                                > > int i = 0;
                                > > int j = strlen (s) - 1;[/color]
                                >
                                > 1) It is not the same initialize a variable as give a variable the
                                > first value it will take.[/color]

                                What are you trying to say here? That code is correct.
                                [color=blue]
                                > 2) Not always a stack to add variables is available, or to add more
                                > variables to it. Sometimes only modify code is allowed (i.e: patching
                                > firmware in real time systems without stop them).[/color]

                                I don't see how this is different from your code since you yourself
                                introduce the variable 'r'.
                                [color=blue]
                                > 3) Relation between names "i" and "j" and their meaning/usage is
                                > totally lost.[/color]

                                Lost? I understood it. The code is short and clear so it is
                                stylistically correct to use simple variable names (n, x, y, i, j
                                etc..). For that matter, the "relation between" 'r' and 's' in your
                                code and "their meaning/usage" also fall into the same category.
                                [color=blue][color=green]
                                > > while (i < j)[/color]
                                >
                                > 4) Never heard about "for" statement?. It is used to enclose in an easy
                                > to read statement all control of the loop iterators (initialization s,
                                > exit condition and state update).[/color]

                                "Easy to read"? Certainly not your code. And in Mark's code "while" is
                                quite natural - use the right tool for the right job.
                                [color=blue][color=green]
                                > > {
                                > > char tmp = s [i];[/color]
                                >
                                > 5) Declare char here, far of the semantically parent of it (char *s) it
                                > is only a way to hide things. And lots of compilers will ignore it (no
                                > new frame).[/color]

                                This is correct and valid in C99 (unlike your code which is incorrect
                                and invalid in any C standard). Just because there are no C99 compilers
                                around doesn't mean that this code is not "C".
                                [color=blue][color=green]
                                > > s [i] = s [j];
                                > > s [j] = tmp;[/color]
                                >
                                > 6) It seems this code must always be compiled with latest version of
                                > advanced compilers. The responsability to convert array index
                                > calculations to pointer operations, even integers to pointers, is
                                > transferred to compiler.[/color]

                                Nope, the above fragment of code will compile on any C compiler, I
                                suspect it is even valid in the original K&R "C" but I'm not sure.
                                [color=blue][color=green]
                                > > works and is easy to understand.[/color]
                                >
                                > 8) "Works" is not a measure of quality.[/color]

                                True, as the experts here have pointed out about YOUR code. But "easy
                                to understand" IS a measure of quality.
                                [color=blue]
                                > This is my last post in this subject. I don not like to be troll, nor
                                > feed trolls.[/color]

                                Mark have not been acting like a troll. He was merely scolding you for
                                writing poor code. You however are increasingly acting in a troll like
                                manner by insisting to argue even when you've been proven wrong.

                                Comment

                                Working...