strtok problem

Discussion in 'C Programming' started by Victor Eijkhout, May 15, 2006.

  1. I've used this function before but I'm suddenly getting bus errors:

    %% cat tok.c
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

    int main(int argc,char **argv)
    {
    char *c,*fs; const char *sep = ",";

    fs = "5,1";
    c = strtok(fs,sep);
    printf("1st: <%s>\n",c);

    return 0;
    }
    %% cc tok.c ; a.out
    Bus error

    According to gdb the problem occurs in strtok, and the program "works"
    if the separator string does not contain characters that are in the test
    string.

    This problem happens on Mac OS X (Apple-modified gcc 4), gcc 3.something
    on Linux, but not with IBM's compiler on a Power5.

    Anyone any idea what is happening here?

    Victor.
    --
    Victor Eijkhout -- eijkhout at tacc utexas edu
    ph: 512 471 5809
    Victor Eijkhout, May 15, 2006
    #1
    1. Advertising

  2. Victor Eijkhout <> wrote:

    > int main(int argc,char **argv)
    > {
    > char *c,*fs; const char *sep = ",";


    > fs = "5,1";


    This line is the problem. Although your compiler will not complain
    about this assignment, since string literals in C are not "const", you
    may not modify string literals. strtok() modifies its first argument,
    so you are illegally attempting to modify a string literal. If the
    separator string isn't found in the test string, the test string is
    not modified; hence the behavior you observed.

    In your example, the problem can be completely solved thus:

    char fs[]="5,1";

    This is not the same as the code you posted; can you see the difference?

    > c = strtok(fs,sep);
    > printf("1st: <%s>\n",c);


    > return 0;
    > }


    > This problem happens on Mac OS X (Apple-modified gcc 4), gcc 3.something
    > on Linux, but not with IBM's compiler on a Power5.


    Modifying a string literal results in undefined behavior, one possible
    manifestation of which is the program working on some platforms but
    not others.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, May 15, 2006
    #2
    1. Advertising

  3. Victor Eijkhout

    pete Guest

    Victor Eijkhout wrote:
    >
    > I've used this function before but I'm suddenly getting bus errors:
    >
    > %% cat tok.c
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(int argc,char **argv)
    > {
    > char *c,*fs; const char *sep = ",";
    >
    > fs = "5,1";
    > c = strtok(fs,sep);
    > printf("1st: <%s>\n",c);
    >
    > return 0;
    > }
    > %% cc tok.c ; a.out
    > Bus error
    >
    > According to gdb the problem occurs in strtok, and the program "works"
    > if the separator string
    > does not contain characters that are in the test
    > string.
    >
    > This problem happens on Mac OS X
    > (Apple-modified gcc 4), gcc 3.something
    > on Linux, but not with IBM's compiler on a Power5.
    >
    > Anyone any idea what is happening here?


    You're attempting to modify a string literal,
    which is undefined to do.

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char *c;
    const char *sep = ",";
    char fs[] = "5,1";

    c = strtok(fs, sep);
    printf("1st: <%s>\n", c);
    return 0;
    }


    --
    pete
    pete, May 15, 2006
    #3
  4. Christopher Benson-Manica <> wrote:

    > strtok() modifies its first argument,


    Oh darn. I knew that......

    > so you are illegally attempting to modify a string literal.


    .....so I should have realized that. Thanks for pointing it out.

    But I'm wondering what happens on a deep level. A string literal is not
    a piece lf malloc'ed memory? What is it, then? Or is it allocated in
    read-only memory, and the OS balks when I try to write in it?

    Victor.
    --
    Victor Eijkhout -- eijkhout at tacc utexas edu
    ph: 512 471 5809
    Victor Eijkhout, May 15, 2006
    #4
  5. Victor Eijkhout

    Vladimir Oka Guest

    Victor Eijkhout opined:

    > Christopher Benson-Manica <> wrote:
    >
    >> strtok() modifies its first argument,

    >
    > Oh darn. I knew that......
    >
    >> so you are illegally attempting to modify a string literal.

    >
    > ....so I should have realized that. Thanks for pointing it out.
    >
    > But I'm wondering what happens on a deep level. A string literal is
    > not a piece lf malloc'ed memory? What is it, then? Or is it allocated
    > in read-only memory, and the OS balks when I try to write in it?


    What happens on a "deep level" is implementation specific. You may be
    able to find out in your documentation or ask where your
    implementation is discussed.

    However, as far as Standard C is concerned, you shouldn't really care,
    apart from the fact that it's illegal to modify string literals.

    [E.g. & OT: an embedded compiler may indeed decide to put string
    literals in physically read-only memory, OTH on most hosted
    implementations it may be in the area of memory which the underlying
    OS protects from being written to].

    --
    There are no threads in a.b.p.erotica, so there's no gain in using a
    threaded news reader.
    (Unknown source)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    Vladimir Oka, May 15, 2006
    #5
  6. Victor Eijkhout wrote:
    >
    > Christopher Benson-Manica <> wrote:
    >
    > > strtok() modifies its first argument,

    >
    > Oh darn. I knew that......
    >
    > > so you are illegally attempting to modify a string literal.

    >
    > ....so I should have realized that. Thanks for pointing it out.
    >
    > But I'm wondering what happens on a deep level. A string literal is not
    > a piece lf malloc'ed memory? What is it, then? Or is it allocated in
    > read-only memory, and the OS balks when I try to write in it?
    >

    These are implementation issues. It does *not* matter how the
    error is detected. The standard says do *not* do it....so do *not*
    do it.

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond richmond at plano dot net |
    +----------------------------------------------------------------+
    Charles Richmond, May 15, 2006
    #6
    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. strtok problem

    , Aug 28, 2003, in forum: C Programming
    Replies:
    4
    Views:
    495
  2. Ram Laxman

    strtok problem

    Ram Laxman, Apr 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    5,084
    Ram Laxman
    May 3, 2004
  3. collinm

    strtok problem - strcmp

    collinm, Mar 24, 2005, in forum: C Programming
    Replies:
    4
    Views:
    780
    Mark McIntyre
    Mar 24, 2005
  4. Replies:
    3
    Views:
    478
    Steven Kobes
    Jul 27, 2005
  5. ern

    strtok() problem

    ern, Sep 20, 2005, in forum: C Programming
    Replies:
    12
    Views:
    623
    Default User
    Sep 22, 2005
Loading...

Share This Page