why debug step by step, it s ok

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baumann@pan

    #1

    why debug step by step, it s ok

    Hi all,


    I have one simple program:
    #define __EXTENSIONS__
    #include <stdio.h>
    #include <string.h>


    int main() {
    char *buf="5/90/45";
    char *token;
    char *lasts;


    printf("tokeniz ing \"%s\" with strtok():\n", buf);
    if ((token = strtok(buf, "/")) != NULL) {
    printf("token = \"%s\"\n", token);
    while ((token = strtok(NULL, "/")) != NULL) {
    printf("token = \"%s\"\n", token);
    }
    }
    }

    the program has abused the strok, i know it,

    ******but why when i `gdb prog_name` and execute step by step , it 's
    ok?


    thanks

    baumann@pan

  • Mark F. Haigh

    #2
    Re: why debug step by step, it s ok

    baumann@pan wrote:[color=blue]
    > Hi all,
    >[/color]

    <code snipped>
    [color=blue]
    > the program has abused the strok, i know it,
    >
    > ******but why when i `gdb prog_name` and execute step by step , it 's
    > ok?
    >[/color]

    Undefined behavior is exactly that: undefined. The code might work
    fine, crash, generate nasal demons, or cause mudslides in California.

    The point is, you don't know.


    Mark F. Haigh
    mfhaigh@sbcglob al.net

    Comment

    • Michael Knaup

      #3
      Re: why debug step by step, it s ok

      baumann@pan wrote:
      [color=blue]
      > Hi all,
      >
      >
      > I have one simple program:
      > #define __EXTENSIONS__
      > #include <stdio.h>
      > #include <string.h>
      >
      >
      > int main() {
      > char *buf="5/90/45";[/color]
      buf is a pointer to a string constant
      char buf[] = "5/90/45"
      is correct.
      [color=blue]
      > char *token;
      > char *lasts;
      >
      >
      > printf("tokeniz ing \"%s\" with strtok():\n", buf);
      > if ((token = strtok(buf, "/")) != NULL) {[/color]
      strtok changes the content of buf, so the content must be changeable.
      [color=blue]
      > printf("token = \"%s\"\n", token);
      > while ((token = strtok(NULL, "/")) != NULL) {
      > printf("token = \"%s\"\n", token);
      > }
      > }
      > }
      >
      > the program has abused the strok, i know it,
      >
      > ******but why when i `gdb prog_name` and execute step by step , it 's
      > ok?
      >
      >
      > thanks
      >
      > baumann@pan[/color]

      --
      Michael Knaup

      Comment

      • baumann@pan

        #4
        Re: why debug step by step, it s ok

        you donn't answer my question,

        i have answered the question in another post thread.

        and in practice, i found when in debug and exec step by step, the prog
        runs well.


        Michael Knaup wrote:[color=blue]
        > baumann@pan wrote:
        >[color=green]
        > > Hi all,
        > >
        > >
        > > I have one simple program:
        > > #define __EXTENSIONS__
        > > #include <stdio.h>
        > > #include <string.h>
        > >
        > >
        > > int main() {
        > > char *buf="5/90/45";[/color]
        > buf is a pointer to a string constant
        > char buf[] = "5/90/45"
        > is correct.
        >[color=green]
        > > char *token;
        > > char *lasts;
        > >
        > >
        > > printf("tokeniz ing \"%s\" with strtok():\n", buf);
        > > if ((token = strtok(buf, "/")) != NULL) {[/color]
        > strtok changes the content of buf, so the content must be changeable.
        >[color=green]
        > > printf("token = \"%s\"\n", token);
        > > while ((token = strtok(NULL, "/")) != NULL) {
        > > printf("token = \"%s\"\n", token);
        > > }
        > > }
        > > }
        > >
        > > the program has abused the strok, i know it,
        > >
        > > ******but why when i `gdb prog_name` and execute step by step , it 's
        > > ok?
        > >
        > >
        > > thanks
        > >
        > > baumann@pan[/color]
        >
        > --
        > Michael Knaup[/color]

        Comment

        • baumann@pan

          #5
          Re: why debug step by step, it s ok

          you can read the post


          the 2nd post i pointed out the problem, the 3rd post, i bring forward
          the problem asked here.

          Michael Knaup wrote:[color=blue]
          > baumann@pan wrote:
          >[color=green]
          > > Hi all,
          > >
          > >
          > > I have one simple program:
          > > #define __EXTENSIONS__
          > > #include <stdio.h>
          > > #include <string.h>
          > >
          > >
          > > int main() {
          > > char *buf="5/90/45";[/color]
          > buf is a pointer to a string constant
          > char buf[] = "5/90/45"
          > is correct.
          >[color=green]
          > > char *token;
          > > char *lasts;
          > >
          > >
          > > printf("tokeniz ing \"%s\" with strtok():\n", buf);
          > > if ((token = strtok(buf, "/")) != NULL) {[/color]
          > strtok changes the content of buf, so the content must be changeable.
          >[color=green]
          > > printf("token = \"%s\"\n", token);
          > > while ((token = strtok(NULL, "/")) != NULL) {
          > > printf("token = \"%s\"\n", token);
          > > }
          > > }
          > > }
          > >
          > > the program has abused the strok, i know it,
          > >
          > > ******but why when i `gdb prog_name` and execute step by step , it 's
          > > ok?
          > >
          > >
          > > thanks
          > >
          > > baumann@pan[/color]
          >
          > --
          > Michael Knaup[/color]

          Comment

          • Michael Knaup

            #6
            Re: why debug step by step, it s ok

            baumann@pan wrote:
            [color=blue]
            > you can read the post
            >[/color]
            http://groups-beta.google.com/group/...000d5e177771c8[color=blue]
            >
            > the 2nd post i pointed out the problem, the 3rd post, i bring forward
            > the problem asked here.
            >
            > Michael Knaup wrote:[color=green]
            >> baumann@pan wrote:
            >>[color=darkred]
            >> > Hi all,
            >> >
            >> >
            >> > I have one simple program:
            >> > #define __EXTENSIONS__
            >> > #include <stdio.h>
            >> > #include <string.h>
            >> >
            >> >
            >> > int main() {
            >> > char *buf="5/90/45";[/color]
            >> buf is a pointer to a string constant
            >> char buf[] = "5/90/45"
            >> is correct.
            >>[color=darkred]
            >> > char *token;
            >> > char *lasts;
            >> >
            >> >
            >> > printf("tokeniz ing \"%s\" with strtok():\n", buf);
            >> > if ((token = strtok(buf, "/")) != NULL) {[/color]
            >> strtok changes the content of buf, so the content must be changeable.
            >>[color=darkred]
            >> > printf("token = \"%s\"\n", token);
            >> > while ((token = strtok(NULL, "/")) != NULL) {
            >> > printf("token = \"%s\"\n", token);
            >> > }
            >> > }
            >> > }
            >> >
            >> > the program has abused the strok, i know it,
            >> >
            >> > ******but why when i `gdb prog_name` and execute step by step , it 's
            >> > ok?
            >> >
            >> >
            >> > thanks
            >> >
            >> > baumann@pan[/color]
            >>
            >> --
            >> Michael Knaup[/color][/color]

            I showed the solution for the problem and M. F. Haigh wrote the
            answer to your question.

            And here, espacially for you, again:

            Modifiying a string literal leads to undefined behavior.
            This means it might work in one execution environment (gdb)
            or crash, or format your disk, or ... in another.

            --
            Michael Knaup

            Comment

            • Michael Knaup

              #7
              Re: why debug step by step, it s ok

              Michael Knaup wrote:

              so where is the difference between

              (1) char *buffer = "Constant Literal";

              and
              (2) char buffer[17] = "Constant Literal";

              In (1) you define a pointer to char in (2) you define an
              array of char's.

              In (1) the pointer points to the constant string literal,
              in (2) the array is initialised with the char's of the
              constant literal. This means (2) is equivalent to
              (2) char buffer[17] = { 'C', 'o', ..., 'l', '\000' };

              And cause the compiler knows the length of the initialising
              string literal in (2) you are not required to give the array
              size explicitly so you can just write
              char buffer[] = "Con...";


              --
              Michael Knaup

              Comment

              • Emmanuel Delahaye

                #8
                Re: why debug step by step, it s ok

                baumann@pan wrote on 03/06/05 :[color=blue]
                > #include <stdio.h>
                > #include <string.h>
                >
                > int main() {
                > char *buf="5/90/45";[/color]

                char buf[]="5/90/45";
                [color=blue]
                > char *token;
                > char *lasts;
                >
                > printf("tokeniz ing \"%s\" with strtok():\n", buf);
                > if ((token = strtok(buf, "/")) != NULL) {
                > printf("token = \"%s\"\n", token);
                > while ((token = strtok(NULL, "/")) != NULL) {
                > printf("token = \"%s\"\n", token);
                > }
                > }
                > }[/color]

                strings are not mutable...

                --
                Emmanuel
                The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                The C-library: http://www.dinkumware.com/refxc.html

                "Mal nommer les choses c'est ajouter du malheur au
                monde." -- Albert Camus.

                Comment

                • Emmanuel Delahaye

                  #9
                  Re: why debug step by step, it s ok

                  baumann@pan wrote on 03/06/05 :[color=blue]
                  > you donn't answer my question,
                  >
                  > i have answered the question in another post thread.
                  >
                  > and in practice, i found when in debug and exec step by step, the prog
                  > runs well.[/color]

                  An Undefined Behaviour has an undefined behavour.

                  --
                  Emmanuel
                  The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                  The C-library: http://www.dinkumware.com/refxc.html

                  "Mal nommer les choses c'est ajouter du malheur au
                  monde." -- Albert Camus.

                  Comment

                  • Robert Gamble

                    #10
                    Re: why debug step by step, it s ok

                    On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:
                    [color=blue]
                    > baumann@pan wrote on 03/06/05 :[color=green]
                    >> #include <stdio.h>
                    >> #include <string.h>
                    >>
                    >> int main() {
                    >> char *buf="5/90/45";[/color]
                    >
                    > char buf[]="5/90/45";
                    >[color=green]
                    >> char *token;
                    >> char *lasts;
                    >>
                    >> printf("tokeniz ing \"%s\" with strtok():\n", buf);
                    >> if ((token = strtok(buf, "/")) != NULL) {
                    >> printf("token = \"%s\"\n", token);
                    >> while ((token = strtok(NULL, "/")) != NULL) {
                    >> printf("token = \"%s\"\n", token);
                    >> }
                    >> }
                    >> }[/color]
                    >
                    > strings are not mutable...[/color]

                    Psst: string _literals_ are not mutable.

                    Robert Gamble

                    Comment

                    • Keith Thompson

                      #11
                      Re: why debug step by step, it s ok

                      Robert Gamble <rgamble99@gmai l.com> writes:[color=blue]
                      > On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:[/color]
                      [...][color=blue][color=green]
                      >> strings are not mutable...[/color]
                      >
                      > Psst: string _literals_ are not mutable.[/color]

                      Psst: Attempting to modify a string literal invokes undefined
                      behavior. One way that undefined behavior can be manifested is that
                      the string literal can be modified.

                      Saying that string literals are not mutable could imply that it's an
                      error that the implementation will catch. It might, or it might not.
                      It's up to the programmer to avoid making the mistake in the first
                      place.

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

                      • August Karlstrom

                        #12
                        Re: why debug step by step, it s ok

                        baumann@pan wrote:[color=blue]
                        > Hi all,
                        >
                        >
                        > I have one simple program:
                        > #define __EXTENSIONS__
                        > #include <stdio.h>
                        > #include <string.h>
                        >
                        >
                        > int main() {
                        > char *buf="5/90/45";
                        > char *token;
                        > char *lasts;
                        >
                        >
                        > printf("tokeniz ing \"%s\" with strtok():\n", buf);
                        > if ((token = strtok(buf, "/")) != NULL) {
                        > printf("token = \"%s\"\n", token);
                        > while ((token = strtok(NULL, "/")) != NULL) {
                        > printf("token = \"%s\"\n", token);
                        > }
                        > }
                        > }
                        >
                        > the program has abused the strok, i know it,
                        >
                        > ******but why when i `gdb prog_name` and execute step by step , it 's
                        > ok?[/color]

                        baumann here clearly states that he knows that the program is incorrect
                        and the reason for it. Many of you reply as if he didn't know that.
                        Please read the post before replying.


                        -- August

                        Comment

                        • Robert Gamble

                          #13
                          Re: why debug step by step, it s ok

                          Keith Thompson wrote:[color=blue]
                          > Robert Gamble <rgamble99@gmai l.com> writes:[color=green]
                          > > On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:[/color]
                          > [...][color=green][color=darkred]
                          > >> strings are not mutable...[/color]
                          > >
                          > > Psst: string _literals_ are not mutable.[/color]
                          >
                          > Psst: Attempting to modify a string literal invokes undefined
                          > behavior. One way that undefined behavior can be manifested is that
                          > the string literal can be modified.[/color]

                          I thought about clarifying that but decided not to assuming that the
                          "without invoking undefined behavior" part was implied and trying not
                          to be too pedantic as I know what Emmanuel meant ;)
                          [color=blue]
                          > Saying that string literals are not mutable could imply that it's an
                          > error that the implementation will catch. It might, or it might not.[/color]

                          I agree, bad decision on my part.

                          Robert Gamble

                          Comment

                          • Barry Schwarz

                            #14
                            Re: why debug step by step, it s ok

                            On 3 Jun 2005 00:37:19 -0700, "baumann@pa n" <baumann.Pan@gm ail.com>
                            wrote:
                            [color=blue]
                            >Hi all,
                            >
                            >
                            >I have one simple program:
                            >#define __EXTENSIONS__
                            >#include <stdio.h>
                            >#include <string.h>
                            >
                            >
                            >int main() {
                            > char *buf="5/90/45";
                            > char *token;
                            > char *lasts;
                            >
                            >
                            > printf("tokeniz ing \"%s\" with strtok():\n", buf);
                            > if ((token = strtok(buf, "/")) != NULL) {
                            > printf("token = \"%s\"\n", token);
                            > while ((token = strtok(NULL, "/")) != NULL) {
                            > printf("token = \"%s\"\n", token);
                            > }
                            > }
                            >}
                            >
                            >the program has abused the strok, i know it,
                            >
                            >******but why when i `gdb prog_name` and execute step by step , it 's
                            >ok?[/color]

                            One of the worst forms of undefined behavior is "pretending to work as
                            intended". This can be compounded by "only failing when being
                            displayed to an important customer or senior management".

                            Undefined means anything can happen. You just experienced one such
                            anything.


                            <<Remove the del for email>>

                            Comment

                            • Emmanuel Delahaye

                              #15
                              Re: why debug step by step, it s ok

                              Robert Gamble wrote on 04/06/05 :[color=blue]
                              > I thought about clarifying that but decided not to assuming that the
                              > "without invoking undefined behavior" part was implied and trying not
                              > to be too pedantic as I know what Emmanuel meant ;)[/color]

                              Ok, I rephrase it:

                              "The C langage doesn't guarantee that a string literal is mutable".

                              --
                              Emmanuel
                              The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                              The C-library: http://www.dinkumware.com/refxc.html

                              "Mal nommer les choses c'est ajouter du malheur au
                              monde." -- Albert Camus.

                              Comment

                              Working...