Re: segmentation fault in strtok_r()
From: Artie Gold (artiegold_at_austin.rr.com)
Date: 07/30/03
- Next message: JohnWu: "Re: How to list the first 10 lines error from executing command"
- Previous message: Daniel Shaw: "Problem with configure script in Unix network programming book by Stevens"
- In reply to: qazmlp: "segmentation fault in strtok_r()"
- Next in thread: Paul Pluzhnikov: "Re: segmentation fault in strtok_r()"
- Reply: Paul Pluzhnikov: "Re: segmentation fault in strtok_r()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: JohnWu: "Re: How to list the first 10 lines error from executing command"
- Previous message: Daniel Shaw: "Problem with configure script in Unix network programming book by Stevens"
- In reply to: qazmlp: "segmentation fault in strtok_r()"
- Next in thread: Paul Pluzhnikov: "Re: segmentation fault in strtok_r()"
- Reply: Paul Pluzhnikov: "Re: segmentation fault in strtok_r()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|