Re: segmentation fault in strtok_r()

From: Artie Gold (artiegold_at_austin.rr.com)
Date: 07/30/03


Date: Wed, 30 Jul 2003 01:38:28 GMT

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


Relevant Pages

  • Re: segmentation fault in strtok_r()
    ... qazmlp wrote: ... > There is a segmentation fault when running the executable of the ... > Please suggest a fix for this. ... > pstack core info: ...
    (comp.unix.solaris)
  • Re: gdb SIGSEGV
    ... > I get this error info when I debug a segmentation fault using gdb: ... > Program received signal SIGSEGV, ... > What to fix this? ... have to compile your program with debugging options turned on. ...
    (comp.lang.c)
  • kernel BUG: loading acpi_cpufreq causes segmentation fault with 2.6.20-RC7-RT3
    ... Loading the acpi_cpufreq module causes a segmentation fault with ... Does anyone know how to fix this? ... kernel BUG at drivers/acpi/osl.c:373! ...
    (Linux-Kernel)
  • Re: gdb SIGSEGV
    ... Segmentation fault 0x42073d65 ... Fix the invalid pointer/subscript. ... Morris Dovey ...
    (comp.lang.c)