segmentation fault in strtok_r()

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default segmentation fault in strtok_r()

    There is a segmentation fault when running the executable of the
    following code.
    Please suggest a fix for this.

    I compiled the code with: "CC -mt file.C"
    pstack core info:
    core 'core' of 26704: a.out
    ----------------- lwp# 1 / thread# 1 --------------------
    ff24db7c strtok_r (10c9d, 10c84, ffbeeb2c, ff2bbc60, 6d, ff00) + 58
    00010a4c char*trimLeadEndSpace(char*) (0, 0, 0, 219b8, 0, 0) + 8c
    00010b5c main (1, ffbeec04, ffbeec0c, 20c00, 0, 0) + c
    00010998 _start (0, 0, 0, 0, 0, 0) + 108
    ----------------- lwp# 2 / thread# 2 --------------------
    ff2999ec _signotifywait (ff30e000, 5c, 0, 0, 0, 0) + 8
    ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    ----------------- lwp# 3 --------------------------------
    ff2975b0 _door_return (ff1e5d78, 0, 6000, ffbee6bc, 0, 0) + 10
    ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    -------------------------- thread# 3 --------------------
    ff2ed8e8 _reap_wait (ff312a30, 209f4, 0, ff30e000, 0, 0) + 38
    ff2ed640 _reaper (ff30ee58, ff314798, ff312a30, ff30ee30, 1,
    fe400000) + 38
    ff2fbb34 _thread_start (0, 0, 0, 0, 0, 0) + 40


    // Code Starts here
    #include <string.h>
    #include <stdio.h>

    char * trimLeadEndSpace( char * pCharArray )
    {
    printf("pCharArray=%s", pCharArray ) ;
    char *AddrWOWhiteSpace = 0 ;
    char *pPTR = 0 , *ppPTR = 0 ;
    char *pTOK = 0 , *ppTOK = 0 ;
    char *dummy = 0 ;

    if( pCharArray == NULL ) return NULL ;

    int Len = strlen(pCharArray) ;
    dummy = new char [ Len + 1 ] ;
    strcpy( dummy , pCharArray ) ;


    int len = 0 , index = 0;

    //
    // here we gather all the tokens and add up each length to
    // give the total length of the string w/o white-spaces
    //
    printf("pCharArray=%s", pCharArray ) ;
    pTOK = strtok_r( pCharArray , " " , &pPTR );
    printf("pCharArray=%s", pCharArray ) ;
    while( pTOK != NULL ){
    index++;
    len = len + strlen(pTOK) ;
    pTOK = strtok_r( NULL , " " , &pPTR );
    }

    // here we prep a mem location enough for the final
    // string w/o any white spaces
    //
    AddrWOWhiteSpace = new char [len + 1];
    AddrWOWhiteSpace[0] = '\0' ;


    //
    // here we concat all the tokens
    //
    index = 0 ;
    ppTOK = strtok_r( dummy , " " , &ppPTR );
    while( ppTOK != NULL )
    {
    strncat( AddrWOWhiteSpace , ppTOK , strlen(ppTOK) ) ;

    index++;
    ppTOK = strtok_r( NULL , " " , &ppPTR );
    }

    delete [] dummy ;

    return (AddrWOWhiteSpace) ;
    }

    int main()
    {
    printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    project=code% A F" ) ) ;
    }
    qazmlp Guest

  2. Similar Questions and Discussions

    1. Segmentation Fault
      I am running a PHP script from the command line that is returning "Segmentation Fault". I run a lot of script from the command line and have never...
    2. [PHP] Segmentation Fault
      I am running a PHP script from the command line that is returning "Segmentation Fault". I run a lot of script from the command line and have ...
    3. #25045 [Opn->Bgs]: Segmentation fault
      ID: 25045 Updated by: iliaa@php.net Reported By: bvaughan at fame dot com -Status: Open +Status: ...
    4. #24907 [Opn->Bgs]: Segmentation Fault
      ID: 24907 Updated by: magnus@php.net Reported By: rarteaga at icaro dot com dot ec -Status: Open +Status: ...
    5. 1.8.0 Segmentation fault
      I have been experiencing problems with the 1.8.0 (MSVC build) causing segmentation faults. In an effort to track down the problem I downloaded the...
  3. #2

    Default segmentation fault in strtok_r()

    There is a segmentation fault when running the executable of the
    following code.
    Please suggest a fix for this.

    I compiled the code with: "CC -mt file.C"
    pstack core info:
    core 'core' of 26704: a.out
    ----------------- lwp# 1 / thread# 1 --------------------
    ff24db7c strtok_r (10c9d, 10c84, ffbeeb2c, ff2bbc60, 6d, ff00) + 58
    00010a4c char*trimLeadEndSpace(char*) (0, 0, 0, 219b8, 0, 0) + 8c
    00010b5c main (1, ffbeec04, ffbeec0c, 20c00, 0, 0) + c
    00010998 _start (0, 0, 0, 0, 0, 0) + 108
    ----------------- lwp# 2 / thread# 2 --------------------
    ff2999ec _signotifywait (ff30e000, 5c, 0, 0, 0, 0) + 8
    ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    ----------------- lwp# 3 --------------------------------
    ff2975b0 _door_return (ff1e5d78, 0, 6000, ffbee6bc, 0, 0) + 10
    ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    -------------------------- thread# 3 --------------------
    ff2ed8e8 _reap_wait (ff312a30, 209f4, 0, ff30e000, 0, 0) + 38
    ff2ed640 _reaper (ff30ee58, ff314798, ff312a30, ff30ee30, 1,
    fe400000) + 38
    ff2fbb34 _thread_start (0, 0, 0, 0, 0, 0) + 40


    // Code Starts here
    #include <string.h>
    #include <stdio.h>

    char * trimLeadEndSpace( char * pCharArray )
    {
    printf("pCharArray=%s", pCharArray ) ;
    char *AddrWOWhiteSpace = 0 ;
    char *pPTR = 0 , *ppPTR = 0 ;
    char *pTOK = 0 , *ppTOK = 0 ;
    char *dummy = 0 ;

    if( pCharArray == NULL ) return NULL ;

    int Len = strlen(pCharArray) ;
    dummy = new char [ Len + 1 ] ;
    strcpy( dummy , pCharArray ) ;


    int len = 0 , index = 0;

    //
    // here we gather all the tokens and add up each length to
    // give the total length of the string w/o white-spaces
    //
    printf("pCharArray=%s", pCharArray ) ;
    pTOK = strtok_r( pCharArray , " " , &pPTR );
    printf("pCharArray=%s", pCharArray ) ;
    while( pTOK != NULL ){
    index++;
    len = len + strlen(pTOK) ;
    pTOK = strtok_r( NULL , " " , &pPTR );
    }

    // here we prep a mem location enough for the final
    // string w/o any white spaces
    //
    AddrWOWhiteSpace = new char [len + 1];
    AddrWOWhiteSpace[0] = '\0' ;


    //
    // here we concat all the tokens
    //
    index = 0 ;
    ppTOK = strtok_r( dummy , " " , &ppPTR );
    while( ppTOK != NULL )
    {
    strncat( AddrWOWhiteSpace , ppTOK , strlen(ppTOK) ) ;

    index++;
    ppTOK = strtok_r( NULL , " " , &ppPTR );
    }

    delete [] dummy ;

    return (AddrWOWhiteSpace) ;
    }

    int main()
    {
    printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    project=code% A F" ) ) ;
    }
    qazmlp Guest

  4. #3

    Default Re: segmentation fault in strtok_r()

    qazmlp wrote:
    > There is a segmentation fault when running the executable of the
    > following code.
    > Please suggest a fix for this.
    >
    > I compiled the code with: "CC -mt file.C"
    > pstack core info:
    > core 'core' of 26704: a.out
    > ----------------- lwp# 1 / thread# 1 --------------------
    > ff24db7c strtok_r (10c9d, 10c84, ffbeeb2c, ff2bbc60, 6d, ff00) + 58
    > 00010a4c char*trimLeadEndSpace(char*) (0, 0, 0, 219b8, 0, 0) + 8c
    > 00010b5c main (1, ffbeec04, ffbeec0c, 20c00, 0, 0) + c
    > 00010998 _start (0, 0, 0, 0, 0, 0) + 108
    > ----------------- lwp# 2 / thread# 2 --------------------
    > ff2999ec _signotifywait (ff30e000, 5c, 0, 0, 0, 0) + 8
    > ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    > ----------------- lwp# 3 --------------------------------
    > ff2975b0 _door_return (ff1e5d78, 0, 6000, ffbee6bc, 0, 0) + 10
    > ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    > -------------------------- thread# 3 --------------------
    > ff2ed8e8 _reap_wait (ff312a30, 209f4, 0, ff30e000, 0, 0) + 38
    > ff2ed640 _reaper (ff30ee58, ff314798, ff312a30, ff30ee30, 1,
    > fe400000) + 38
    > ff2fbb34 _thread_start (0, 0, 0, 0, 0, 0) + 40
    >
    >
    > // Code Starts here
    > #include <string.h>
    > #include <stdio.h>
    >
    > char * trimLeadEndSpace( char * pCharArray )
    > {
    > printf("pCharArray=%s", pCharArray ) ;
    > char *AddrWOWhiteSpace = 0 ;
    > char *pPTR = 0 , *ppPTR = 0 ;
    > char *pTOK = 0 , *ppTOK = 0 ;
    > char *dummy = 0 ;
    >
    > if( pCharArray == NULL ) return NULL ;
    >
    > int Len = strlen(pCharArray) ;
    > dummy = new char [ Len + 1 ] ;
    > strcpy( dummy , pCharArray ) ;
    >
    >
    > int len = 0 , index = 0;
    >
    > //
    > // here we gather all the tokens and add up each length to
    > // give the total length of the string w/o white-spaces
    > //
    > printf("pCharArray=%s", pCharArray ) ;
    > pTOK = strtok_r( pCharArray , " " , &pPTR );
    > printf("pCharArray=%s", pCharArray ) ;
    > while( pTOK != NULL ){
    > index++;
    > len = len + strlen(pTOK) ;
    > pTOK = strtok_r( NULL , " " , &pPTR );
    strtok_r requires a pointer to space that _exists_.
    Please see the man page.
    > }
    >
    > // here we prep a mem location enough for the final
    > // string w/o any white spaces
    > //
    > AddrWOWhiteSpace = new char [len + 1];
    > AddrWOWhiteSpace[0] = '\0' ;
    >
    >
    > //
    > // here we concat all the tokens
    > //
    > index = 0 ;
    > ppTOK = strtok_r( dummy , " " , &ppPTR );
    Similarly.
    > while( ppTOK != NULL )
    > {
    > strncat( AddrWOWhiteSpace , ppTOK , strlen(ppTOK) ) ;
    >
    > index++;
    > ppTOK = strtok_r( NULL , " " , &ppPTR );
    > }
    >
    > delete [] dummy ;
    >
    > return (AddrWOWhiteSpace) ;
    > }
    >
    > int main()
    > {
    > printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    > project=code% A F" ) ) ;
    > }
    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas

    Artie Gold Guest

  5. #4

    Default Re: segmentation fault in strtok_r()

    qazmlp wrote:
    > There is a segmentation fault when running the executable of the
    > following code.
    > Please suggest a fix for this.
    >
    > I compiled the code with: "CC -mt file.C"
    > pstack core info:
    > core 'core' of 26704: a.out
    > ----------------- lwp# 1 / thread# 1 --------------------
    > ff24db7c strtok_r (10c9d, 10c84, ffbeeb2c, ff2bbc60, 6d, ff00) + 58
    > 00010a4c char*trimLeadEndSpace(char*) (0, 0, 0, 219b8, 0, 0) + 8c
    > 00010b5c main (1, ffbeec04, ffbeec0c, 20c00, 0, 0) + c
    > 00010998 _start (0, 0, 0, 0, 0, 0) + 108
    > ----------------- lwp# 2 / thread# 2 --------------------
    > ff2999ec _signotifywait (ff30e000, 5c, 0, 0, 0, 0) + 8
    > ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    > ----------------- lwp# 3 --------------------------------
    > ff2975b0 _door_return (ff1e5d78, 0, 6000, ffbee6bc, 0, 0) + 10
    > ff2f1c88 thr_yield (0, 0, 0, 0, 0, 0) + 8c
    > -------------------------- thread# 3 --------------------
    > ff2ed8e8 _reap_wait (ff312a30, 209f4, 0, ff30e000, 0, 0) + 38
    > ff2ed640 _reaper (ff30ee58, ff314798, ff312a30, ff30ee30, 1,
    > fe400000) + 38
    > ff2fbb34 _thread_start (0, 0, 0, 0, 0, 0) + 40
    >
    >
    > // Code Starts here
    > #include <string.h>
    > #include <stdio.h>
    >
    > char * trimLeadEndSpace( char * pCharArray )
    > {
    > printf("pCharArray=%s", pCharArray ) ;
    > char *AddrWOWhiteSpace = 0 ;
    > char *pPTR = 0 , *ppPTR = 0 ;
    > char *pTOK = 0 , *ppTOK = 0 ;
    > char *dummy = 0 ;
    >
    > if( pCharArray == NULL ) return NULL ;
    >
    > int Len = strlen(pCharArray) ;
    > dummy = new char [ Len + 1 ] ;
    > strcpy( dummy , pCharArray ) ;
    >
    >
    > int len = 0 , index = 0;
    >
    > //
    > // here we gather all the tokens and add up each length to
    > // give the total length of the string w/o white-spaces
    > //
    > printf("pCharArray=%s", pCharArray ) ;
    > pTOK = strtok_r( pCharArray , " " , &pPTR );
    > printf("pCharArray=%s", pCharArray ) ;
    > while( pTOK != NULL ){
    > index++;
    > len = len + strlen(pTOK) ;
    > pTOK = strtok_r( NULL , " " , &pPTR );
    strtok_r requires a pointer to space that _exists_.
    Please see the man page.
    > }
    >
    > // here we prep a mem location enough for the final
    > // string w/o any white spaces
    > //
    > AddrWOWhiteSpace = new char [len + 1];
    > AddrWOWhiteSpace[0] = '\0' ;
    >
    >
    > //
    > // here we concat all the tokens
    > //
    > index = 0 ;
    > ppTOK = strtok_r( dummy , " " , &ppPTR );
    Similarly.
    > while( ppTOK != NULL )
    > {
    > strncat( AddrWOWhiteSpace , ppTOK , strlen(ppTOK) ) ;
    >
    > index++;
    > ppTOK = strtok_r( NULL , " " , &ppPTR );
    > }
    >
    > delete [] dummy ;
    >
    > return (AddrWOWhiteSpace) ;
    > }
    >
    > int main()
    > {
    > printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    > project=code% A F" ) ) ;
    > }
    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas

    Artie Gold Guest

  6. #5

    Default Re: segmentation fault in strtok_r()

    Artie Gold <artiegold@austin.rr.com> writes:
    > > pTOK = strtok_r( pCharArray , " " , &pPTR );
    > > printf("pCharArray=%s", pCharArray ) ;
    > > while( pTOK != NULL ){
    > > index++;
    > > len = len + strlen(pTOK) ;
    > > pTOK = strtok_r( NULL , " " , &pPTR );
    >
    > strtok_r requires a pointer to space that _exists_.
    > Please see the man page.
    Huh? I think *you* need to revisit the man page ;-)

    His use of strtok_r() is correct, but
    > > int main()
    > > {
    > > printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    this is a problem: quoted string literals are not writable (and
    strtok_r() *does* write to its argument).

    In fact, CC warned him about it:

    "junk.cc", line 65: Warning: String literal converted to char*
    in formal argument pCharArray in call to trimLeadEndSpace(char*).

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Paul Pluzhnikov Guest

  7. #6

    Default Re: segmentation fault in strtok_r()

    Artie Gold <artiegold@austin.rr.com> writes:
    > > pTOK = strtok_r( pCharArray , " " , &pPTR );
    > > printf("pCharArray=%s", pCharArray ) ;
    > > while( pTOK != NULL ){
    > > index++;
    > > len = len + strlen(pTOK) ;
    > > pTOK = strtok_r( NULL , " " , &pPTR );
    >
    > strtok_r requires a pointer to space that _exists_.
    > Please see the man page.
    Huh? I think *you* need to revisit the man page ;-)

    His use of strtok_r() is correct, but
    > > int main()
    > > {
    > > printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    this is a problem: quoted string literals are not writable (and
    strtok_r() *does* write to its argument).

    In fact, CC warned him about it:

    "junk.cc", line 65: Warning: String literal converted to char*
    in formal argument pCharArray in call to trimLeadEndSpace(char*).

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Paul Pluzhnikov Guest

  8. #7

    Default Re: segmentation fault in strtok_r()

    Artie Gold <artiegold@austin.rr.com> writes:
    > > The strtok_r() function works the same as the strtok()
    > > function, but instead of using a static buffer it uses a
    > > pointer to a user allocated char* pointer. This pointer,
    > > the ptrptr parameter, must be the same while parsing the
    > > same string.
    >
    > Have I gone brain-dead? :-(
    Not completely: you can still type :-)

    He did allocate the 'char*' pointer (on the stack) right here:

    char * trimLeadEndSpace( char * pCharArray )
    { ...
    char *pPTR = 0 , *ppPTR = 0 ;
    ^^^^^^^^^^^^^^^^^

    The ptrptr (from the man page) == &pPTR, and it is 'the same'
    while parsing the same string, so he's done nothing wrong WRT
    strtok_r(), which uses (and changes) the value of pPTR to keep
    track of where in the string to continue tokenizing ...

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Paul Pluzhnikov Guest

  9. #8

    Default Re: segmentation fault in strtok_r()

    Artie Gold <artiegold@austin.rr.com> writes:
    > > The strtok_r() function works the same as the strtok()
    > > function, but instead of using a static buffer it uses a
    > > pointer to a user allocated char* pointer. This pointer,
    > > the ptrptr parameter, must be the same while parsing the
    > > same string.
    >
    > Have I gone brain-dead? :-(
    Not completely: you can still type :-)

    He did allocate the 'char*' pointer (on the stack) right here:

    char * trimLeadEndSpace( char * pCharArray )
    { ...
    char *pPTR = 0 , *ppPTR = 0 ;
    ^^^^^^^^^^^^^^^^^

    The ptrptr (from the man page) == &pPTR, and it is 'the same'
    while parsing the same string, so he's done nothing wrong WRT
    strtok_r(), which uses (and changes) the value of pPTR to keep
    track of where in the string to continue tokenizing ...

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Paul Pluzhnikov Guest

  10. #9

    Default Re: segmentation fault in strtok_r()

    Paul Pluzhnikov wrote:
    > Artie Gold <artiegold@austin.rr.com> writes:
    >
    >
    >>> The strtok_r() function works the same as the strtok()
    >>> function, but instead of using a static buffer it uses a
    >>> pointer to a user allocated char* pointer. This pointer,
    >>> the ptrptr parameter, must be the same while parsing the
    >>> same string.
    >>
    >>Have I gone brain-dead? :-(
    >
    >
    > Not completely: you can still type :-)
    Ah, merely severe cerebral flatulence.
    >
    > He did allocate the 'char*' pointer (on the stack) right here:
    >
    > char * trimLeadEndSpace( char * pCharArray )
    > { ...
    > char *pPTR = 0 , *ppPTR = 0 ;
    > ^^^^^^^^^^^^^^^^^
    >
    > The ptrptr (from the man page) == &pPTR, and it is 'the same'
    > while parsing the same string, so he's done nothing wrong WRT
    > strtok_r(), which uses (and changes) the value of pPTR to keep
    > track of where in the string to continue tokenizing ...
    >
    Of course. ;-/

    --ag

    --
    Artie Gold -- Austin, Texas

    Artie Gold Guest

  11. #10

    Default Re: segmentation fault in strtok_r()

    Paul Pluzhnikov wrote:
    > Artie Gold <artiegold@austin.rr.com> writes:
    >
    >
    >>> The strtok_r() function works the same as the strtok()
    >>> function, but instead of using a static buffer it uses a
    >>> pointer to a user allocated char* pointer. This pointer,
    >>> the ptrptr parameter, must be the same while parsing the
    >>> same string.
    >>
    >>Have I gone brain-dead? :-(
    >
    >
    > Not completely: you can still type :-)
    Ah, merely severe cerebral flatulence.
    >
    > He did allocate the 'char*' pointer (on the stack) right here:
    >
    > char * trimLeadEndSpace( char * pCharArray )
    > { ...
    > char *pPTR = 0 , *ppPTR = 0 ;
    > ^^^^^^^^^^^^^^^^^
    >
    > The ptrptr (from the man page) == &pPTR, and it is 'the same'
    > while parsing the same string, so he's done nothing wrong WRT
    > strtok_r(), which uses (and changes) the value of pPTR to keep
    > track of where in the string to continue tokenizing ...
    >
    Of course. ;-/

    --ag

    --
    Artie Gold -- Austin, Texas

    Artie Gold Guest

  12. #11

    Default Re: segmentation fault in strtok_r()

    [email]qazmlp1209@rediffmail.com[/email] (qazmlp) writes:
    >There is a segmentation fault when running the executable of the
    >following code.
    >Please suggest a fix for this.
    >int main()
    >{
    > printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    >project=code% A F" ) ) ;
    >}
    You call strok() on a a string constant. Those may live in
    read-only memory; strtok_r writes NUL bytes in the string.

    Casper
    Casper H.S. Dik Guest

  13. #12

    Default Re: segmentation fault in strtok_r()

    [email]qazmlp1209@rediffmail.com[/email] (qazmlp) writes:
    >There is a segmentation fault when running the executable of the
    >following code.
    >Please suggest a fix for this.
    >int main()
    >{
    > printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
    >project=code% A F" ) ) ;
    >}
    You call strok() on a a string constant. Those may live in
    read-only memory; strtok_r writes NUL bytes in the string.

    Casper
    Casper H.S. Dik Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139