Ask a Question related to UNIX Programming, Design and Development.
-
qazmlp #1
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
-
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... -
[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 ... -
#25045 [Opn->Bgs]: Segmentation fault
ID: 25045 Updated by: iliaa@php.net Reported By: bvaughan at fame dot com -Status: Open +Status: ... -
#24907 [Opn->Bgs]: Segmentation Fault
ID: 24907 Updated by: magnus@php.net Reported By: rarteaga at icaro dot com dot ec -Status: Open +Status: ... -
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... -
qazmlp #2
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
-
Artie Gold #3
Re: segmentation fault in strtok_r()
qazmlp wrote:
strtok_r requires a pointer to space that _exists_.> 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 );
Please see the man page.
Similarly.> }
>
> // 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 );
HTH,> 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" ) ) ;
> }
--ag
--
Artie Gold -- Austin, Texas
Artie Gold Guest
-
Artie Gold #4
Re: segmentation fault in strtok_r()
qazmlp wrote:
strtok_r requires a pointer to space that _exists_.> 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 );
Please see the man page.
Similarly.> }
>
> // 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 );
HTH,> 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" ) ) ;
> }
--ag
--
Artie Gold -- Austin, Texas
Artie Gold Guest
-
Paul Pluzhnikov #5
Re: segmentation fault in strtok_r()
Artie Gold <artiegold@austin.rr.com> writes:
Huh? I think *you* need to revisit the man page ;-)>> > 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.
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
-
Paul Pluzhnikov #6
Re: segmentation fault in strtok_r()
Artie Gold <artiegold@austin.rr.com> writes:
Huh? I think *you* need to revisit the man page ;-)>> > 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.
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
-
Paul Pluzhnikov #7
Re: segmentation fault in strtok_r()
Artie Gold <artiegold@austin.rr.com> writes:
Not completely: you can still type :-)>> > 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? :-(
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
-
Paul Pluzhnikov #8
Re: segmentation fault in strtok_r()
Artie Gold <artiegold@austin.rr.com> writes:
Not completely: you can still type :-)>> > 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? :-(
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
-
Artie Gold #9
Re: segmentation fault in strtok_r()
Paul Pluzhnikov wrote:
Ah, merely severe cerebral flatulence.> 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 :-)
Of course. ;-/>
> 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 ...
>
--ag
--
Artie Gold -- Austin, Texas
Artie Gold Guest
-
Artie Gold #10
Re: segmentation fault in strtok_r()
Paul Pluzhnikov wrote:
Ah, merely severe cerebral flatulence.> 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 :-)
Of course. ;-/>
> 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 ...
>
--ag
--
Artie Gold -- Austin, Texas
Artie Gold Guest
-
Casper H.S. Dik #11
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.You call strok() on a a string constant. Those may live in>int main()
>{
> printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
>project=code% A F" ) ) ;
>}
read-only memory; strtok_r writes NUL bytes in the string.
Casper
Casper H.S. Dik Guest
-
Casper H.S. Dik #12
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.You call strok() on a a string constant. Those may live in>int main()
>{
> printf("%s", trimLeadEndSpace( ":myAppAddress@hotmail.com ;
>project=code% A F" ) ) ;
>}
read-only memory; strtok_r writes NUL bytes in the string.
Casper
Casper H.S. Dik Guest



Reply With Quote

