segfault on strtok

Discussion in 'C Programming' started by Fatih Gey, Oct 23, 2003.

  1. Fatih Gey

    Fatih Gey Guest

    Hi,

    .. following causes a segfault. .. didn't
    know why ?!

    int main() {
    char name[15];
    strcpy (name, "ab8bc8cd8ed");

    char cur[800];
    strcpy (cur, strtok(name, "8"));

    while (cur) {
    printf ("Output: %s\n", cur);
    printf ("Stringlength %i\n", strlen(cur));
    strcpy(cur,strtok(0, "8"));
    printf ("next\n");
    }

    }

    output: -----------------------

    Output: ab
    Stringlength 2
    next
    Output: bc
    Stringlength 2
    next
    Output: cd
    Stringlength 2
    next
    Output: ed
    Stringlength 2
    Segmentationfault
    ------------------------------

    Thanks for any help
    Regards
    Fatih Gey
     
    Fatih Gey, Oct 23, 2003
    #1
    1. Advertising

  2. In article <bn9kks$jov$03$-online.com>, Fatih Gey wrote:
    [cut]

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

    > int main() {
    > char name[15];
    > strcpy (name, "ab8bc8cd8ed");
    >
    > char cur[800];
    > strcpy (cur, strtok(name, "8"));
    >
    > while (cur) {


    cur is never going to be zero.

    > printf ("Output: %s\n", cur);
    > printf ("Stringlength %i\n", strlen(cur));
    > strcpy(cur,strtok(0, "8"));


    (strictly speaking, the zero on that line should be a null
    pointer)

    > printf ("next\n");
    > }
    >
    > }



    Check the return value of strtok(). It returns NULL when no
    more tokens remains. NULL is defined in <stdlib.h>.

    Cheers,
    Andreas


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 23, 2003
    #2
    1. Advertising

  3. Andreas Kahari wrote:
    > In article <bn9kks$jov$03$-online.com>, Fatih Gey wrote:
    >
    >> printf ("Output: %s\n", cur);
    >> printf ("Stringlength %i\n", strlen(cur));
    >> strcpy(cur,strtok(0, "8"));

    >
    >
    > (strictly speaking, the zero on that line should be a null
    > pointer)


    0 converted to a pointer *is* a null pointer. There's nothing wrong with
    using 0 here.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Oct 24, 2003
    #3
  4. Fatih Gey

    pete Guest

    Andreas Kahari wrote:
    >
    > In article <bn9kks$jov$03$-online.com>, Fatih Gey wrote:
    > [cut]
    >
    > #include <stdio.h>
    > #include <string.h>


    > NULL is defined in <stdlib.h>.


    NULL is also defined in stdio.h and string.h,
    as well as in other header files.

    stddef.h has the NULL macro and the _t typedefs.
    NULL and all of the other macros and typedefs,
    are also defined in each header file that has a prototype
    for any function, which is defined as either
    being able to accept them [NULL and friends], as arguments,
    or return them as values.

    --
    pete
     
    pete, Oct 24, 2003
    #4
  5. In article <>, pete wrote:
    > Andreas Kahari wrote:

    [cut]
    >> NULL is defined in <stdlib.h>.

    >
    > NULL is also defined in stdio.h and string.h,
    > as well as in other header files.



    Ah, yes.


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 24, 2003
    #5
  6. In article
    <OlZlb.3553$>, Kevin
    Goodsell wrote:
    > Andreas Kahari wrote:

    [cut]
    >> (strictly speaking, the zero on that line should be a null
    >> pointer)

    >
    > 0 converted to a pointer *is* a null pointer. There's nothing wrong with
    > using 0 here.


    Yes, the integer zero is a null pointer, but only if it's cast
    to void * and converted into a pointer type. Besides, it looks
    better if one says NULL where one means NULL.

    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 24, 2003
    #6
  7. On Thu, 23 Oct 2003 23:35:08 +0000, Andreas Kahari wrote:

    > In article
    > <OlZlb.3553$>, Kevin
    > Goodsell wrote:
    >> Andreas Kahari wrote:
    >>>> strcpy(cur,strtok(0, "8"));
    >>> (strictly speaking, the zero on that line should be a null
    >>> pointer)

    >>
    >> 0 converted to a pointer *is* a null pointer. There's nothing wrong with
    >> using 0 here.

    >
    > Yes, the integer zero is a null pointer, but only if it's cast
    > to void * and converted into a pointer type.


    The first argument to strtok() above is not "the integer zero". It
    is a null pointer constant and there is no need to cast it to
    void *

    In fact, NULL is often defined like this:

    #define NULL 0

    > Besides, it looks better if one says NULL where one means NULL.


    That is a matter of opinion.
     
    Sheldon Simms, Oct 24, 2003
    #7
  8. Fatih Gey

    Fatih Gey Guest

    >
    > #include <stdio.h>
    > #include <string.h>
    >
    >>int main() {
    >> char name[15];
    >> strcpy (name, "ab8bc8cd8ed");
    >>
    >> char cur[800];
    >> strcpy (cur, strtok(name, "8"));
    >>
    >> while (cur) {

    >
    >
    > cur is never going to be zero.
    >
    >
    >> printf ("Output: %s\n", cur);
    >> printf ("Stringlength %i\n", strlen(cur));
    >> strcpy(cur,strtok(0, "8"));

    >
    >
    > (strictly speaking, the zero on that line should be a null
    > pointer)
    >
    >
    >> printf ("next\n");
    >> }
    >>
    >>}

    >
    >
    >
    > Check the return value of strtok(). It returns NULL when no
    > more tokens remains. NULL is defined in <stdlib.h>.
    >

    ok .. i added the both include-lines, changed
    while (cur) {
    in
    while (cur != NULL) {
    and replaced 0 with NULL in subsequenced strtok-func, but it keeps
    producing segfaults :-(

    I think, the problem causes when while run last time and strtok returns
    NULL. Is there anything wrong with strcpy-ing this NULL pointer to var
    "cur" ? (see also the output-wrap in first msg of thread)


    Fatih Gey
     
    Fatih Gey, Oct 24, 2003
    #8
  9. In article <bnak4r$7qi$06$-online.com>, Fatih Gey wrote:
    [cut]
    > ok .. i added the both include-lines, changed
    > while (cur) {
    > in
    > while (cur != NULL) {
    > and replaced 0 with NULL in subsequenced strtok-func, but it keeps
    > producing segfaults :-(
    >
    > I think, the problem causes when while run last time and strtok returns
    > NULL. Is there anything wrong with strcpy-ing this NULL pointer to var
    > "cur" ? (see also the output-wrap in first msg of thread)


    Yes, that's a problem since strcpy() will probably try to
    dereference the opinters given to it, and you can't dereference
    a NULL pointer. Assign the retun value from strtok() to a char
    pointer and check its value before using it with strcpy().


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 24, 2003
    #9
  10. Fatih Gey

    pete Guest

    Andreas Kahari wrote:

    > Yes, the integer zero is a null pointer, but only if it's cast
    > to void * and converted into a pointer type.


    A constant expression with a value of zero,
    is called a "null pointer constant"
    (you can find that phrase in the standard),
    whther or not it's cast to (void*).

    --
    pete
     
    pete, Oct 24, 2003
    #10
  11. Fatih Gey

    Al Bowers Guest

    Fatih Gey wrote:
    > Hi,
    >
    > .. following causes a segfault. .. didn't
    > know why ?!
    >
    > int main() {
    > char name[15];
    > strcpy (name, "ab8bc8cd8ed");
    >
    > char cur[800];
    > strcpy (cur, strtok(name, "8"));
    >
    > while (cur) {
    > printf ("Output: %s\n", cur);
    > printf ("Stringlength %i\n", strlen(cur));
    > strcpy(cur,strtok(0, "8"));
    > printf ("next\n");
    > }
    >
    > }


    Once all the delimiters, '8' in this case, are exhausted, function
    strtok will return NULL. At the point of the seg fault your
    statement:
    strcpy(cur,strtok(0,"8"));
    becomes
    strcpy(cur,NULL);
    The second argument NULL is not defined for function strcpy.
    The argument must be a string. Therein lies your seg fault.

    Actually, if your intent is to only print the tokens, you should
    just use a char * instead of using the character array cur.

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

    int main(void)
    {
    char name[15], *cur;

    strcpy (name, "ab8bc8cd8ed");
    cur = strtok(name, "8");
    while(cur)
    {
    printf ("Output: %s\n", cur);
    printf ("Stringlength %u\n", strlen(cur));
    printf ("next\n");
    cur = strtok(NULL,"8");
    }
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Oct 24, 2003
    #11
  12. In article <>, pete wrote:
    > Andreas Kahari wrote:
    >
    >> Yes, the integer zero is a null pointer, but only if it's cast
    >> to void * and converted into a pointer type.

    >
    > A constant expression with a value of zero,
    > is called a "null pointer constant"
    > (you can find that phrase in the standard),
    > whther or not it's cast to (void*).


    I was looking at the standard when I wrote it. It says that an
    integer which is zero is a null pointer constant if cast to void *.
    That null pointer constant is a null pointer if converted to
    a pointer type.

    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 24, 2003
    #12
  13. Fatih Gey

    pete Guest

    Andreas Kahari wrote:
    >
    > In article <>, pete wrote:
    > > Andreas Kahari wrote:
    > >
    > >> Yes, the integer zero is a null pointer, but only if it's cast
    > >> to void * and converted into a pointer type.

    > >
    > > A constant expression with a value of zero,
    > > is called a "null pointer constant"
    > > (you can find that phrase in the standard),
    > > whther or not it's cast to (void*).

    >
    > I was looking at the standard when I wrote it. It says that an
    > integer which is zero is a null pointer constant if cast to void *.
    > That null pointer constant is a null pointer if converted to
    > a pointer type.


    I read it differently from the way that you do.

    N869
    6.3.2.3 Pointers
    [#3]
    An integer constant expression with the value 0,
    or such an expression cast to type void *,
    is called a null pointer constant.

    --
    pete
     
    pete, Oct 24, 2003
    #13
  14. In article <>, pete wrote:
    > Andreas Kahari wrote:

    [cut]
    >> I was looking at the standard when I wrote it. It says that an
    >> integer which is zero is a null pointer constant if cast to void *.
    >> That null pointer constant is a null pointer if converted to
    >> a pointer type.

    > I read it differently from the way that you do.
    >
    > N869
    > 6.3.2.3 Pointers
    > [#3]
    > An integer constant expression with the value 0,
    > or such an expression cast to type void *,
    > is called a null pointer constant.



    That's the draft standard. I have the book version of the
    current standard, but it's at home at the moment. I'll have
    to get back to this issue later tonight if noone else has the
    current standard at hand and can verify this.


    Cheers,
    Andreas

    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 24, 2003
    #14
  15. Fatih Gey

    Al Bowers Guest

    Andreas Kahari wrote:
    > In article <>, pete wrote:
    >
    >>Andreas Kahari wrote:
    >>
    >>
    >>>Yes, the integer zero is a null pointer, but only if it's cast
    >>>to void * and converted into a pointer type.

    >>
    >>A constant expression with a value of zero,
    >>is called a "null pointer constant"
    >>(you can find that phrase in the standard),
    >>whther or not it's cast to (void*).

    >
    >
    > I was looking at the standard when I wrote it. It says that an
    > integer which is zero is a null pointer constant if cast to void *.
    > That null pointer constant is a null pointer if converted to
    > a pointer type.
    >


    I believe what you are referring to in the standard is the
    section 6.3.2.3 Pointers.
    Paragraph:
    3. An integer constant expression with the value 0, or such an
    expression cast to type void *, is called a null pointer constant.

    Notice the word "or". It does not say "only if cast".
    Among other posibilities, NULL may be defined as:
    0, 0L, 0LL, or (void *)0.

    ---
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Oct 24, 2003
    #15
  16. Fatih Gey

    Paul Hsieh Guest

    "Fatih Gey" <> wrote:
    > ...
    > char cur[800];
    > strcpy (cur, strtok(name, "8"));
    >
    > while (cur) {


    This is a constant loop -- i.e., the loop will never end.

    > printf ("Output: %s\n", cur);
    > printf ("Stringlength %i\n", strlen(cur));
    > strcpy(cur,strtok(0, "8"));


    strtok eventually will return NULL. strcpy cannot accept a parameter which is NULL.

    > printf ("next\n");
    > }
    > ...


    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
     
    Paul Hsieh, Oct 24, 2003
    #16
  17. On Fri, 24 Oct 2003 12:55:34 +0000, Andreas Kahari wrote:

    > In article <>, pete wrote:
    >> Andreas Kahari wrote:

    > [cut]
    >>> I was looking at the standard when I wrote it. It says that an
    >>> integer which is zero is a null pointer constant if cast to void *.
    >>> That null pointer constant is a null pointer if converted to
    >>> a pointer type.

    >> I read it differently from the way that you do.
    >>
    >> N869
    >> 6.3.2.3 Pointers
    >> [#3]
    >> An integer constant expression with the value 0,
    >> or such an expression cast to type void *,
    >> is called a null pointer constant.

    >
    >
    > That's the draft standard. I have the book version of the
    > current standard, but it's at home at the moment. I'll have
    > to get back to this issue later tonight if noone else has the
    > current standard at hand and can verify this.


    ISO/IEC 9899:1999
    6.3.2.3 Pointers
    3 An integer constant expression with the value 0, or such an
    expressioncast to type void *, is called a null pointer
    constant.
     
    Sheldon Simms, Oct 24, 2003
    #17
  18. In article <>, Sheldon Simms wrote:
    > On Fri, 24 Oct 2003 12:55:34 +0000, Andreas Kahari wrote:

    [cut]
    >> That's the draft standard. I have the book version of the
    >> current standard, but it's at home at the moment. I'll have
    >> to get back to this issue later tonight if noone else has the
    >> current standard at hand and can verify this.

    > ISO/IEC 9899:1999
    > 6.3.2.3 Pointers
    > 3 An integer constant expression with the value 0, or such an
    > expressioncast to type void *, is called a null pointer
    > constant.



    Yeah yeah, alright.

    I wonder what they mean by "or such an expression" though...


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 24, 2003
    #18
  19. Fatih Gey

    Dan Pop Guest

    In <> Andreas Kahari <> writes:

    >In article <>, pete wrote:
    >> Andreas Kahari wrote:

    >[cut]
    >>> I was looking at the standard when I wrote it. It says that an
    >>> integer which is zero is a null pointer constant if cast to void *.
    >>> That null pointer constant is a null pointer if converted to
    >>> a pointer type.

    >> I read it differently from the way that you do.
    >>
    >> N869
    >> 6.3.2.3 Pointers
    >> [#3]
    >> An integer constant expression with the value 0,
    >> or such an expression cast to type void *,
    >> is called a null pointer constant.

    >
    >That's the draft standard.


    It doesn't matter: the text hasn't changed at least since the last
    public C89 draft, released 15 years ago.

    It makes sense to object to N869 quotations *only* when you know that
    either C89 or C99 say something different.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 24, 2003
    #19
  20. In article <bnbchs$a01$>, Dan Pop wrote:
    > In <> Andreas
    > Kahari <> writes:

    [cut]
    >>That's the draft standard.

    >
    > It doesn't matter: the text hasn't changed at least since the last
    > public C89 draft, released 15 years ago.
    >
    > It makes sense to object to N869 quotations *only* when you know that
    > either C89 or C99 say something different.



    I agree, but I thought I remembered the text differently.
    Others proved me wrong though.


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 24, 2003
    #20
    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. Adam Balgach
    Replies:
    2
    Views:
    576
    news-east
    Nov 28, 2004
  2. Alex Vinokur

    strtok() and std::string

    Alex Vinokur, Apr 14, 2005, in forum: C++
    Replies:
    6
    Views:
    4,939
    Pete Becker
    Apr 14, 2005
  3. strtok problem

    , Aug 28, 2003, in forum: C Programming
    Replies:
    4
    Views:
    518
  4. Robert

    strtok trouble

    Robert, Sep 5, 2003, in forum: C Programming
    Replies:
    17
    Views:
    1,230
    Jalapeno
    Sep 6, 2003
  5. Andrey Vul
    Replies:
    8
    Views:
    698
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page