why debug step by step, it s ok

Discussion in 'C Programming' started by baumann@pan, Jun 3, 2005.

  1. baumann@pan

    baumann@pan Guest

    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("tokenizing \"%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
    baumann@pan, Jun 3, 2005
    #1
    1. Advertising

  2. baumann@pan wrote:
    > Hi all,
    >


    <code snipped>

    > the program has abused the strok, i know it,
    >
    > ******but why when i `gdb prog_name` and execute step by step , it 's
    > ok?
    >


    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
    Mark F. Haigh, Jun 3, 2005
    #2
    1. Advertising

  3. baumann@pan wrote:

    > Hi all,
    >
    >
    > I have one simple program:
    > #define __EXTENSIONS__
    > #include <stdio.h>
    > #include <string.h>
    >
    >
    > int main() {
    > char *buf="5/90/45";

    buf is a pointer to a string constant
    char buf[] = "5/90/45"
    is correct.

    > char *token;
    > char *lasts;
    >
    >
    > printf("tokenizing \"%s\" with strtok():\n", buf);
    > if ((token = strtok(buf, "/")) != NULL) {

    strtok changes the content of buf, so the content must be changeable.

    > 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


    --
    Michael Knaup
    Michael Knaup, Jun 3, 2005
    #3
  4. baumann@pan

    baumann@pan Guest

    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:
    > baumann@pan wrote:
    >
    > > Hi all,
    > >
    > >
    > > I have one simple program:
    > > #define __EXTENSIONS__
    > > #include <stdio.h>
    > > #include <string.h>
    > >
    > >
    > > int main() {
    > > char *buf="5/90/45";

    > buf is a pointer to a string constant
    > char buf[] = "5/90/45"
    > is correct.
    >
    > > char *token;
    > > char *lasts;
    > >
    > >
    > > printf("tokenizing \"%s\" with strtok():\n", buf);
    > > if ((token = strtok(buf, "/")) != NULL) {

    > strtok changes the content of buf, so the content must be changeable.
    >
    > > 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

    >
    > --
    > Michael Knaup
    baumann@pan, Jun 3, 2005
    #4
  5. baumann@pan

    baumann@pan Guest

    you can read the post
    http://groups-beta.google.com/group...abde6a37a51/e7000d5e177771c8#e7000d5e177771c8

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

    Michael Knaup wrote:
    > baumann@pan wrote:
    >
    > > Hi all,
    > >
    > >
    > > I have one simple program:
    > > #define __EXTENSIONS__
    > > #include <stdio.h>
    > > #include <string.h>
    > >
    > >
    > > int main() {
    > > char *buf="5/90/45";

    > buf is a pointer to a string constant
    > char buf[] = "5/90/45"
    > is correct.
    >
    > > char *token;
    > > char *lasts;
    > >
    > >
    > > printf("tokenizing \"%s\" with strtok():\n", buf);
    > > if ((token = strtok(buf, "/")) != NULL) {

    > strtok changes the content of buf, so the content must be changeable.
    >
    > > 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

    >
    > --
    > Michael Knaup
    baumann@pan, Jun 3, 2005
    #5
  6. baumann@pan wrote:

    > you can read the post
    >

    http://groups-beta.google.com/group...abde6a37a51/e7000d5e177771c8#e7000d5e177771c8
    >
    > the 2nd post i pointed out the problem, the 3rd post, i bring forward
    > the problem asked here.
    >
    > Michael Knaup wrote:
    >> baumann@pan wrote:
    >>
    >> > Hi all,
    >> >
    >> >
    >> > I have one simple program:
    >> > #define __EXTENSIONS__
    >> > #include <stdio.h>
    >> > #include <string.h>
    >> >
    >> >
    >> > int main() {
    >> > char *buf="5/90/45";

    >> buf is a pointer to a string constant
    >> char buf[] = "5/90/45"
    >> is correct.
    >>
    >> > char *token;
    >> > char *lasts;
    >> >
    >> >
    >> > printf("tokenizing \"%s\" with strtok():\n", buf);
    >> > if ((token = strtok(buf, "/")) != NULL) {

    >> strtok changes the content of buf, so the content must be changeable.
    >>
    >> > 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

    >>
    >> --
    >> Michael Knaup


    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
    Michael Knaup, Jun 3, 2005
    #6
  7. 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
    Michael Knaup, Jun 3, 2005
    #7
  8. baumann@pan wrote on 03/06/05 :
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main() {
    > char *buf="5/90/45";


    char buf[]="5/90/45";

    > char *token;
    > char *lasts;
    >
    > printf("tokenizing \"%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);
    > }
    > }
    > }


    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.
    Emmanuel Delahaye, Jun 3, 2005
    #8
  9. baumann@pan wrote on 03/06/05 :
    > 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.


    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.
    Emmanuel Delahaye, Jun 3, 2005
    #9
  10. On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:

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

    >
    > char buf[]="5/90/45";
    >
    >> char *token;
    >> char *lasts;
    >>
    >> printf("tokenizing \"%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);
    >> }
    >> }
    >> }

    >
    > strings are not mutable...


    Psst: string _literals_ are not mutable.

    Robert Gamble
    Robert Gamble, Jun 3, 2005
    #10
  11. Robert Gamble <> writes:
    > On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:

    [...]
    >> strings are not mutable...

    >
    > Psst: string _literals_ are not mutable.


    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_Keith) <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.
    Keith Thompson, Jun 3, 2005
    #11
  12. baumann@pan wrote:
    > 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("tokenizing \"%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?


    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
    August Karlstrom, Jun 3, 2005
    #12
  13. Keith Thompson wrote:
    > Robert Gamble <> writes:
    > > On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:

    > [...]
    > >> strings are not mutable...

    > >
    > > Psst: string _literals_ are not mutable.

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


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

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


    I agree, bad decision on my part.

    Robert Gamble
    Robert Gamble, Jun 4, 2005
    #13
  14. On 3 Jun 2005 00:37:19 -0700, "baumann@pan" <>
    wrote:

    >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("tokenizing \"%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?


    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>>
    Barry Schwarz, Jun 4, 2005
    #14
  15. Robert Gamble wrote on 04/06/05 :
    > 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 ;)


    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.
    Emmanuel Delahaye, Jun 4, 2005
    #15
  16. "Emmanuel Delahaye" <> writes:
    > Robert Gamble wrote on 04/06/05 :
    >> 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 ;)

    >
    > Ok, I rephrase it:
    >
    > "The C langage doesn't guarantee that a string literal is mutable".


    Another way to put it is that you can't go wrong assuming that string
    literals are immutable.

    That's not quite true if you're trying to figure out why code that
    modifies a string literal *seems* to be working, but it's a good rule
    for writing new code.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Jun 4, 2005
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jaap de verwant slachter
    Replies:
    0
    Views:
    1,248
    jaap de verwant slachter
    Jul 1, 2003
  2. Roy in

    need step by step example

    Roy in, Aug 3, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    349
    Roy in
    Aug 3, 2003
  3. Steve Richter

    a step by step page

    Steve Richter, May 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    367
    Steve Richter
    May 3, 2005
  4. craig dicker
    Replies:
    1
    Views:
    353
    Peter Rilling
    Jul 10, 2005
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,769
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page