String reversing problem

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

    #31
    Re: String reversing problem

    slebetman@yahoo .com wrote:[color=blue]
    > tmp123 wrote:[color=green]
    > > Hi,
    > >
    > > See inlines:
    > >
    > > Christian Bau wrote:[color=darkred]
    > > > tmp123 wrote:
    > > > > 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.
    > > >
    > > > 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=green][color=darkred]
    > > > 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=green]
    > > 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=green]
    > > 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=green][color=darkred]
    > > > 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=green][color=darkred]
    > > > {
    > > > 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=green][color=darkred]
    > > > 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=green][color=darkred]
    > > > 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=green]
    > > 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.[/color]

    Oh crap, I thought tmp123 was referring to Mark. I meant Christian.
    Sorry for the identity mix-up. My comments still stand though.

    Comment

    • Flash Gordon

      #32
      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]

      If it is a learning exercise, then you should appreciate the advice not
      dismiss it because it is only a learning exercise.
      [color=blue]
      > 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=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 you are saying makes no sense. Initialising a variable *is* giving
      it the first value it will take!
      [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]

      What has that got to do with writing the code properly first time?
      [color=blue]
      > 3) Relation between names "i" and "j" and their meaning/usage is
      > totally lost.[/color]

      i and j are conventionally used as loop iterators. You will just have to
      get used to it if you are going to read other peoples code.
      [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]

      I'm sure he has, but there is nothing intrinsically wrong with what
      Christian Bau has done. In a case like this I'm in two minds about
      whether to do it the way he has done it or using a for loop and not
      initialising the variables on declaration since I can see arguments both
      ways.
      [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]

      It is called localising scope and is often considered a good thing.
      Whether or not the compiler generates a new stack frame (which is
      unlikely) is irrelevant.
      [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]

      Compilers have been doing this king of optimisation for over 10 years,
      so I hardly think a modern compiler is required.
      [color=blue][color=green]
      >> ++i;
      >> --j;[/color]
      >
      > 7) Lost lines here?[/color]

      What lost lines? I see nothing missing.
      [color=blue][color=green]
      >> }
      >> }
      >>
      >> works and is easy to understand.[/color]
      >
      > 8) "Works" is not a measure of quality.[/color]

      So you consider a program that does not work to be of the same quality
      as one that does work? That is just plain stupid. His code is of far
      higher quality by all measures I would use than your code is.
      [color=blue]
      > This is my last post in this subject. I don not like to be troll, nor
      > feed trolls.[/color]

      Well, Christian Bau is not a troll. He posted a good example of how to
      implement the function in order to assist you. If you cannot see that
      then that is your problem.
      --
      Flash Gordon
      Living in interesting times.
      Although my email address says spam, it is real and I read it.

      Comment

      • Flash Gordon

        #33
        Re: String reversing problem

        slebetman@yahoo .com wrote:[color=blue]
        > tmp123 wrote:[color=green]
        >> Hi,
        >>
        >> See inlines:
        >>
        >> Christian Bau wrote:[color=darkred]
        >>> tmp123 wrote:
        >>>> To Mr. Christian Bau:[/color][/color][/color]

        <snip>
        [color=blue][color=green][color=darkred]
        >>> {
        >>> 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]

        Actually, you declaring a variable at the start of any block is valid
        for all versions of C. Also, there are I believe a few complete C99
        implementations , just not many.
        [color=blue][color=green][color=darkred]
        >>> 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]

        It is valid for all versions.
        [color=blue][color=green][color=darkred]
        >>> 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]

        <snip>

        Whether code works is definitely one of the measures of quality, just
        not the only one. If code does not work it is poor quality even if it is
        easy to read. For example, here is a very low quality, but easy to read,
        implementation of a program to copy standard input to standard output.
        #include <stdio.h>
        int main(FILE *standard_input , FILE *standard_outpu t)
        {
        standard_output = standard_input;
        }

        Nice and simple, easy to read, compiles under gcc (with -ansi -pedantic)
        without warning or error, but probably one of the lowest quality
        programs I've ever seen. I could also do ones that are (by some measure)
        just as low quality but do not invoke undefined behaviour.
        --
        Flash Gordon
        Living in interesting times.
        Although my email address says spam, it is real and I read it.

        Comment

        • Emmanuel Delahaye

          #34
          Re: String reversing problem

          Albert a écrit :[color=blue]
          > void reverse(char s[], int num_elements)
          > {
          > int i, j;
          >
          > for (i=0,j=num_elem ents-1; (i<=num_element s-1) && (j>=0); i++,j--)
          > s[i] = s[j];
          > }[/color]

          You need a swap action, hence a local variable.

          --
          A+

          Emmanuel Delahaye

          Comment

          • slebetman@yahoo.com

            #35
            Re: String reversing problem

            Flash Gordon wrote:[color=blue]
            > slebetman@yahoo .com wrote:[color=green]
            > > tmp123 wrote:[color=darkred]
            > >> Hi,
            > >>
            > >> See inlines:
            > >>
            > >> Christian Bau wrote:
            > >>> tmp123 wrote:
            > >>>> To Mr. Christian Bau:[/color][/color]
            >
            > <snip>
            >[color=green][color=darkred]
            > >>> {
            > >>> char tmp = s [i];
            > >> 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]
            >
            > Actually, you declaring a variable at the start of any block is valid
            > for all versions of C.[/color]

            Oh wow, tested it and it does work. I've always thought that "start of
            block" refers to the start of the function. Learn something new
            everyday. This is indeed nice as localising scope is usually a "good
            thing"(tm).

            Comment

            • Mark McIntyre

              #36
              Re: String reversing problem

              On 31 Dec 2005 03:05:17 -0800, in comp.lang.c , "tmp123"
              <tmp123@menta.n et> wrote:
              [color=blue]
              >Christian Bau wrote:[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]

              Thats highly garbled, but I' guessing you're complaining that
              initialising is not the same as assigning a value. So what?
              [color=blue]
              >2) Not always a stack to add variables is available,[/color]

              irrelevant since your own example also introduced variables
              [color=blue]
              >3) Relation between names "i" and "j" and their meaning/usage is
              >totally lost.[/color]

              Rubbish. And your own version had the same mysterious loop variables.
              If you object to that, call them start and end or somthing.
              [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]

              You're objecting to a while loop ? You're either a knave or a fool.
              [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.[/color]

              No, its a way to simplify the code.
              [color=blue]
              >And lots of compilers will ignore it (no new frame).[/color]

              Absolute rubbish.
              [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]

              Rubbish. This is perfectly correct C, and always has been.
              [color=blue]
              >7) Lost lines here?[/color]

              No, it seems you simply don't understand the function.
              [color=blue]
              >8) "Works" is not a measure of quality.[/color]

              Its one measure. If it doesn't work, then its useless rubbish code,
              no?
              [color=blue]
              >This is my last post in this subject. I don not like to be troll,[/color]

              Then stop behaving like one.

              Happy New Year by the way.
              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

              • Netocrat

                #37
                Re: String reversing problem

                On Sat, 31 Dec 2005 12:26:04 +0000, Flash Gordon wrote:
                [a refutation of tmp123's objections to a reverse_string( ) implementation]

                I agree with most of your response, but I want to take up a couple of
                points. Most of tmp123's legitimate objections related to style, except
                for his indexing comment. Reading between the lines, his objections seem
                to result in a rewriting of the function to something like this:

                void reverse_string_ mod(char* s)
                {
                char tmp, *ps, *pe;

                if (*s != '\0')
                {
                for (ps = s, pe = s + strlen (s) - 1; ps < pe; ps++, pe--) {
                tmp = *ps;
                *ps = *pe;
                *pe = tmp;
                }
                }
                }


                Where Christian Bau's original function was:

                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;
                }
                }

                Due to the need for the zero-length test, there is no reduction in
                vertical space - this possibility is what I believe tmp123 was implying
                could be achieved when he wrote "Lost lines here?" (it was an obscure
                wording though and I may have misinterpreted it).

                As you (Flash Gordon) wrote:[color=blue]
                > In a case like this I'm in two minds about whether to do it the way he
                > [Christian Bau] has done it or using a for loop and not initialising the
                > variables on declaration since I can see arguments both ways.[/color]

                Likewise; although the reduction in vertical space and the clustering of
                all looping information in one spot seem good reasons to prefer the for
                loop style.

                Vertical space is something I've come to be conservative about in C code,
                since I find that - all other things equal, and with the qualification
                that occasional empty lines for semantic demarcation are useful - the less
                vertical scrolling/eye-movement required, the faster I can take in the
                code's meaning. For this reason I prefer a bracing style that doesn't
                place starting braces on a new line, and that avoids braces where it's
                possible to do so without otherwise reducing readability.

                [re a claim that indexing requires the compiler to optimise compared to
                pointer arithmetic][color=blue]
                > Compilers have been doing this king of optimisation for over 10 years,
                > so I hardly think a modern compiler is required.[/color]

                The equivalent pointer-arithmetic code is both a well-accepted idiom and
                optimal at an abstract level (at abstract level the additional indexing
                calculations are always performed), and for those reasons I find it
                preferable.

                The optimisations of modern compilers are often unpredictable - doing some
                checking using gcc and icc on my machine, I find that the
                pointer-arithmetic version is /generally/ slightly faster across
                optimisation levels and string lengths, but not always.

                For comment, a vertically-minimalist re-writing of the pointer-arithmetic
                version of the function:

                void reverse_string_ min_vert(char *s) {
                char tmp, *ps, *pe;

                if (*s != '\0')
                for (ps = s, pe = s + strlen(s) - 1; ps < pe; ps++, pe--)
                tmp = *ps, *ps = *pe, *pe = tmp;
                }

                Pros: very little vertical eye movement required; one line per high-level
                operation (test for non-zero length, loop over the string, swap a
                character on each iteration)
                Cons: wide horizontal spacing; a lot of code to digest on each line; the
                omitted braces may reduce the code's readability for those who are used to
                mandatory braces.

                Would this pass review at your shop? Why/why not?

                --

                Comment

                • Flash Gordon

                  #38
                  Re: String reversing problem

                  Netocrat wrote:[color=blue]
                  > On Sat, 31 Dec 2005 12:26:04 +0000, Flash Gordon wrote:
                  > [a refutation of tmp123's objections to a reverse_string( ) implementation][/color]

                  <snip>
                  [color=blue]
                  > Due to the need for the zero-length test, there is no reduction in
                  > vertical space - this possibility is what I believe tmp123 was implying
                  > could be achieved when he wrote "Lost lines here?" (it was an obscure
                  > wording though and I may have misinterpreted it).[/color]

                  Very obscure if that is what it meant..
                  [color=blue]
                  > As you (Flash Gordon) wrote:[/color]

                  <snip>
                  [color=blue]
                  > [re a claim that indexing requires the compiler to optimise compared to
                  > pointer arithmetic][color=green]
                  >> Compilers have been doing this king of optimisation for over 10 years,
                  >> so I hardly think a modern compiler is required.[/color]
                  >
                  > The equivalent pointer-arithmetic code is both a well-accepted idiom and
                  > optimal at an abstract level (at abstract level the additional indexing
                  > calculations are always performed), and for those reasons I find it
                  > preferable.
                  >
                  > The optimisations of modern compilers are often unpredictable - doing some
                  > checking using gcc and icc on my machine, I find that the
                  > pointer-arithmetic version is /generally/ slightly faster across
                  > optimisation levels and string lengths, but not always.[/color]

                  Yes, but:
                  1) It is only the highest optimisation levels (one for speed and one for
                  space) that count IMHO, and you've not specified if there is any
                  difference at that point.
                  2) Readability and maintainability are far more important.

                  For this, I might have used either indexing or pointers, and would be
                  unlikely to comment on the choice made at a review.
                  [color=blue]
                  > For comment, a vertically-minimalist re-writing of the pointer-arithmetic
                  > version of the function:
                  >
                  > void reverse_string_ min_vert(char *s) {
                  > char tmp, *ps, *pe;
                  >
                  > if (*s != '\0')
                  > for (ps = s, pe = s + strlen(s) - 1; ps < pe; ps++, pe--)
                  > tmp = *ps, *ps = *pe, *pe = tmp;
                  > }
                  >
                  > Pros: very little vertical eye movement required; one line per high-level
                  > operation (test for non-zero length, loop over the string, swap a
                  > character on each iteration)
                  > Cons: wide horizontal spacing; a lot of code to digest on each line; the
                  > omitted braces may reduce the code's readability for those who are used to
                  > mandatory braces.
                  >
                  > Would this pass review at your shop? Why/why not?[/color]

                  I would not reject it out of hand but for me there is too much occurring
                  on each line and therefore takes me longer to read.
                  --
                  Flash Gordon
                  Living in interesting times.
                  Although my email address says spam, it is real and I read it.

                  Comment

                  • tmp123

                    #39
                    Re: String reversing problem

                    slebetman@yahoo .com wrote:[color=blue][color=green]
                    > >[color=darkred]
                    > > >>> {
                    > > >>> char tmp = s [i];
                    > > >> 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).
                    > > >
                    > > > 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]
                    > >
                    > > Actually, you declaring a variable at the start of any block is valid
                    > > for all versions of C.[/color]
                    >
                    > Oh wow, tested it and it does work. I've always thought that "start of
                    > block" refers to the start of the function. Learn something new
                    > everyday. This is indeed nice as localising scope is usually a "good
                    > thing"(tm).[/color]


                    "works" only "more or less". Imagine you have a function with two big
                    locals.The first one is used at the start of function, the second one
                    at the end. You can thing this version saves stack space:

                    void test ( void )
                    {
                    {
                    char tmp1[10000];
                    ... some code
                    }
                    {
                    char tmp2[10000];
                    ... more code
                    }
                    }

                    However, if you display the address of tmp1 and tmp2, you will see that
                    lots of compilers converts it to:
                    void test ( void )
                    {
                    char tmp1[10000];
                    char tmp2[10000];
                    ... some code
                    ... more code
                    }

                    that is not the expected one. That doesn't means this resource must not
                    be used. But it is good to known what will happen.

                    Kind regards.

                    Comment

                    • slebetman@yahoo.com

                      #40
                      Re: String reversing problem

                      tmp123 wrote:[color=blue]
                      > slebetman@yahoo .com wrote:[color=green][color=darkred]
                      > > >
                      > > > >>> {
                      > > > >>> char tmp = s [i];
                      > > > >> 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).
                      > > > >
                      > > > > 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".
                      > > >
                      > > > Actually, you declaring a variable at the start of any block is valid
                      > > > for all versions of C.[/color]
                      > >
                      > > Oh wow, tested it and it does work. I've always thought that "start of
                      > > block" refers to the start of the function. Learn something new
                      > > everyday. This is indeed nice as localising scope is usually a "good
                      > > thing"(tm).[/color]
                      >
                      >
                      > "works" only "more or less". Imagine you have a function with two big
                      > locals.The first one is used at the start of function, the second one
                      > at the end. You can thing this version saves stack space:
                      >
                      > void test ( void )
                      > {
                      > {
                      > char tmp1[10000];
                      > ... some code
                      > }
                      > {
                      > char tmp2[10000];
                      > ... more code
                      > }
                      > }
                      >
                      > However, if you display the address of tmp1 and tmp2, you will see that
                      > lots of compilers converts it to:
                      > void test ( void )
                      > {
                      > char tmp1[10000];
                      > char tmp2[10000];
                      > ... some code
                      > ... more code
                      > }
                      >[/color]

                      At the assembly level maybe but at the "C" level not true. Try
                      compiling:

                      #include <stdio.h>
                      int main()
                      {
                      int i;
                      for (i=0;i<10;i++) {
                      int n = i * 2;
                      printf("%d\n", n);
                      }
                      n = 3;
                      printf("%d\n", n);
                      }

                      and you'll get:

                      testprog.c: In function `main':
                      testprog.c:14: error: `n' undeclared (first use in this function)
                      testprog.c:14: error: (Each undeclared identifier is reported only once
                      testprog.c:14: error: for each function it appears in.)

                      Localising scope has little to do with trying to save memory but have
                      more to do with protecting variables from being misused. Think of the
                      difference of local and global, only in this case we get to use
                      variables that are more 'local' than local (if you know what I mean).

                      The first language I encountered this feature is in Perl. Little did I
                      know that C had it all along.

                      Comment

                      • tmp123

                        #41
                        New frames and scopes

                        Hi,

                        See inlines

                        Kind regards.

                        slebetman@yahoo .com wrote:[color=blue]
                        > tmp123 wrote:[color=green][color=darkred]
                        > > >
                        > > > Oh wow, tested it and it does work. I've always thought that "start of
                        > > > block" refers to the start of the function. Learn something new
                        > > > everyday. This is indeed nice as localising scope is usually a "good
                        > > > thing"(tm).[/color]
                        > >
                        > >
                        > > "works" only "more or less". Imagine you have a function with two big
                        > > locals.The first one is used at the start of function, the second one
                        > > at the end. You can thing this version saves stack space:
                        > >
                        > > void test ( void )
                        > > {
                        > > {
                        > > char tmp1[10000];
                        > > ... some code
                        > > }
                        > > {
                        > > char tmp2[10000];
                        > > ... more code
                        > > }
                        > > }
                        > >
                        > > However, if you display the address of tmp1 and tmp2, you will see that
                        > > lots of compilers converts it to:
                        > > void test ( void )
                        > > {
                        > > char tmp1[10000];
                        > > char tmp2[10000];
                        > > ... some code
                        > > ... more code
                        > > }
                        > >[/color]
                        >
                        > At the assembly level maybe but at the "C" level not true. Try
                        > compiling:
                        >
                        > #include <stdio.h>
                        > int main()
                        > {
                        > int i;
                        > for (i=0;i<10;i++) {
                        > int n = i * 2;
                        > printf("%d\n", n);
                        > }
                        > n = 3;
                        > printf("%d\n", n);
                        > }
                        >
                        > and you'll get:
                        >
                        > testprog.c: In function `main':
                        > testprog.c:14: error: `n' undeclared (first use in this function)
                        > testprog.c:14: error: (Each undeclared identifier is reported only once
                        > testprog.c:14: error: for each function it appears in.)[/color]

                        Thanks for showing the difference between variable scope, visibility,
                        life... . It can be interesting for some readers.
                        [color=blue]
                        >
                        > Localising scope has little to do with trying to save memory but have
                        > more to do with protecting variables from being misused. Think of the
                        > difference of local and global, only in this case we get to use
                        > variables that are more 'local' than local (if you know what I mean).
                        >[/color]

                        See this function:

                        /* swap pairs of characters in strings: 12345 => 21435 */
                        void reverse ( char *s )
                        {
                        /* end condition */
                        if ( s[0]=='\0' || s[1]=='\0' ) return;

                        /* swap values */
                        {
                        char tmp;
                        tmp=s[0];
                        s[0]=s[1];
                        s[1]=tmp;
                        }

                        reverse(s+2);
                        }

                        [color=blue]
                        > The first language I encountered this feature is in Perl. Little did I
                        > know that C had it all along.[/color]

                        There are older languages than perl with it.

                        Kind regards.

                        Comment

                        • Emmanuel Delahaye

                          #42
                          Re: String reversing problem

                          tmp123 a écrit :[color=blue]
                          > 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=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]

                          The effect is similar. What exactly is your point ?
                          [color=blue]
                          > 2) Not always a stack to add variables is available,[/color]

                          Correct, but a C implementation requires automatic memory space for the
                          purpose. If there is no automatic memory available, the implementation
                          is not compliant.
                          [color=blue]
                          > 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]

                          Sounds to be a twisted way of thinking...
                          [color=blue]
                          > 3) Relation between names "i" and "j" and their meaning/usage is
                          > totally lost.[/color]

                          Ok, I could have used ir and iw instead (standing for read/write indexes).
                          [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]

                          It's debatable. I personally prefer to use the for() constructs for
                          'canonic' loops

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

                          For other usages (like the one here), I find while() more appropriate,
                          specially at debug stage.
                          [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]

                          I failed to understand your point. What exactly is wrong here ? All I
                          could say is that I should have used int instead of char.
                          [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]

                          So what ? It seems that you are having hard time to find a real failiure
                          in Christian's code. Never mind, making a fool of yourself in public was
                          definitely your choice.

                          --
                          A+

                          Emmanuel Delahaye

                          Comment

                          • Emmanuel Delahaye

                            #43
                            Re: String reversing problem

                            tmp123 a écrit :[color=blue]
                            > "works" only "more or less". Imagine you have a function with two big
                            > locals.The first one is used at the start of function, the second one
                            > at the end. You can thing this version saves stack space:
                            >
                            > void test ( void )
                            > {
                            > {
                            > char tmp1[10000];
                            > ... some code
                            > }
                            > {
                            > char tmp2[10000];
                            > ... more code
                            > }
                            > }
                            >
                            > However, if you display the address of tmp1 and tmp2, you will see that
                            > lots of compilers converts it to:
                            > void test ( void )
                            > {
                            > char tmp1[10000];
                            > char tmp2[10000];
                            > ... some code
                            > ... more code
                            > }
                            >
                            > that is not the expected one. That doesn't means this resource must not
                            > be used. But it is good to known what will happen.[/color]

                            This is completely a compiler issue. The C language semantics allows the
                            optimization, but the implementors are free to optimize or not.

                            --
                            A+

                            Emmanuel Delahaye

                            Comment

                            • tmp123

                              #44
                              Re: String reversing problem

                              Emmanuel Delahaye wrote:[color=blue]
                              > tmp123 a écrit :[color=green]
                              >>[/color][/color]

                              [...]
                              [color=blue][color=green]
                              > > 2) Not always a stack to add variables is available,[/color]
                              >
                              > Correct, but a C implementation requires automatic memory space for the
                              > purpose. If there is no automatic memory available, the implementation
                              > is not compliant.
                              >[color=green]
                              > > 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]
                              >
                              > Sounds to be a twisted way of thinking...[/color]

                              No, it sounds like a real situation. Imagine you have a lot of machines
                              controling some public service of your country. Imagine these machines
                              have one process with lots of threads, each one controlling one user
                              session. Now, ops, you find an error on code: two variables must be
                              swap.

                              What you do? Stop all country? Update a few code is easy, but better
                              not to change in a live system the stack structure. Thus, use a trick
                              like a^=b^... could be the difference between a big problem and a
                              critical problem.

                              Moreover, in the previous post there was a lot of person saying "to
                              swap you need a local variable". Well, like it is explained in the
                              first level of any good programming course, there are more options.
                              [color=blue]
                              >[color=green]
                              > > 3) Relation between names "i" and "j" and their meaning/usage is
                              > > totally lost.[/color]
                              >
                              > Ok, I could have used ir and iw instead (standing for read/write indexes).
                              >[/color]

                              They are perfectly logical names... taking into account we are talking
                              about a swap of data!
                              [color=blue][color=green]
                              > >[color=darkred]
                              > >> 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]
                              >
                              > It's debatable. I personally prefer to use the for() constructs for
                              > 'canonic' loops
                              >
                              > for (i = 0; i < n; i++)
                              >
                              > For other usages (like the one here), I find while() more appropriate,[/color]

                              It seems you know more than the authors of C. Why not remove the "for"
                              syntax and change it to "for <lv> = 1 TO <expr>". It is preferable for
                              you?.
                              [color=blue]
                              > specially at debug stage.[/color]

                              And after debug you modify code?
                              [color=blue]
                              >
                              >
                              > So what ? It seems that you are having hard time to find a real failiure
                              > in Christian's code. Never mind, making a fool of yourself in public was
                              > definitely your choice.[/color]

                              But never ignorant.
                              [color=blue]
                              >
                              > --
                              > A+
                              >
                              > Emmanuel Delahaye[/color]

                              Kind regards.

                              Comment

                              • Flash Gordon

                                #45
                                Re: String reversing problem

                                tmp123 wrote:[color=blue]
                                > Emmanuel Delahaye wrote:[color=green]
                                >> tmp123 a écrit :[/color]
                                >
                                > [...]
                                >[color=green][color=darkred]
                                >>> 2) Not always a stack to add variables is available,[/color]
                                >> Correct, but a C implementation requires automatic memory space for the
                                >> purpose. If there is no automatic memory available, the implementation
                                >> is not compliant.
                                >>[color=darkred]
                                >>> 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]
                                >> Sounds to be a twisted way of thinking...[/color]
                                >
                                > No, it sounds like a real situation. Imagine you have a lot of machines
                                > controling some public service of your country. Imagine these machines
                                > have one process with lots of threads, each one controlling one user
                                > session. Now, ops, you find an error on code: two variables must be
                                > swap.[/color]

                                How often do you come across a bug where the correct action is just to
                                swap to variables? I've come across lots of bugs but can't think of a
                                time when it was a bug significant to require a live patch where that
                                would solve it.
                                [color=blue]
                                > What you do? Stop all country? Update a few code is easy, but better
                                > not to change in a live system the stack structure. Thus, use a trick
                                > like a^=b^... could be the difference between a big problem and a
                                > critical problem.[/color]

                                So how are you going to actually change the code on this running system
                                without breaking it? Especially as it is highly unlikely to be in a
                                place where you have left a load of noops in that you can just
                                overwrite. If there are, then you can just read one, push it on the
                                stack, move the other, and pop the one you save off and write it back.
                                I've yet to come across any situation when that would not be acceptable.
                                Mind you, as I say, the chances of you being able to patch the code for
                                for that on a live system are negligible.

                                BTW, I have been involved in patching live systems, but it was where we
                                knew there was the possibility of needing to patch things and we could
                                design things in such a way as to specifically allow it. It also
                                required working at the assembler level rather than in C, and I can't
                                see how the tricks we applied could be used at the C level.
                                [color=blue]
                                > Moreover, in the previous post there was a lot of person saying "to
                                > swap you need a local variable". Well, like it is explained in the
                                > first level of any good programming course, there are more options.[/color]

                                In assembler programming it might sometimes make sense to use another
                                method, I've yet to see an instance where it would have made sense in
                                any higher level language, including in C.

                                <snip>
                                [color=blue][color=green][color=darkred]
                                >>>> while (i < j)
                                >>>
                                >>> 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]
                                >> It's debatable. I personally prefer to use the for() constructs for
                                >> 'canonic' loops
                                >>
                                >> for (i = 0; i < n; i++)
                                >>
                                >> For other usages (like the one here), I find while() more appropriate,[/color]
                                >
                                > It seems you know more than the authors of C. Why not remove the "for"
                                > syntax and change it to "for <lv> = 1 TO <expr>". It is preferable for
                                > you?.[/color]

                                Because then you could not do a list traversal for loop possibly.
                                Anyway, since no one uses every facility of any language, it should
                                harldy be a surprise when someone says they would not use a particular
                                facility in some specific way.
                                [color=blue][color=green]
                                >> specially at debug stage.[/color]
                                >
                                > And after debug you modify code?[/color]

                                I doubt he would.
                                [color=blue][color=green]
                                >> So what ? It seems that you are having hard time to find a real failiure
                                >> in Christian's code. Never mind, making a fool of yourself in public was
                                >> definitely your choice.[/color]
                                >
                                > But never ignorant.[/color]

                                Actually, you do sound ignorant. Whether you are or not is a completely
                                different matter.
                                --
                                Flash Gordon
                                Living in interesting times.
                                Although my email address says spam, it is real and I read it.

                                Comment

                                Working...