Why the SEGFAULT in this code?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Why the SEGFAULT in this code?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Why the SEGFAULT in this code?  
RJGraham


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-04 02:21 AM

Can someone tell me why the code below should segfault at the line
indicated?

Also, why does strtok_r() require a **ptrptr and not a *ptr?
(Maybe this is a clue to my problem and misunderstanding of how strtok_r
is used...).

Thanks for any insight.

-Randy

PS: str arg is always guaranteed to point to a valid string < 127 chars


void getNameValuePairFromString(char * str, string & name, string & value)
{
char * buf = (char *) malloc(128);
char * tok = strtok_r(str, " =\n\r", &buf);
if (tok != NULL)
{
name = tok;
tok = strtok_r(NULL, " =\n\r", &buf);
if (tok != NULL)
value = tok;
}
free(buf); // seg fault here !!!
}





[ Post a follow-up to this message ]



    Re: Why the SEGFAULT in this code?  
RJGraham


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-04 02:21 AM

Måns Rullgård wrote:
> RJGraham <null@null.com> writes:
>
> 
[snip][vbcol=seagreen]
>
> strtok_r modifies the value of buf, hence the need for the double
> pointer.  You should save the original value somewhere and pass that
> to free().
>
> 
>
>
Yes, thanks.

I realized that just after posting, which always seems to happen ;)

-Randy





[ Post a follow-up to this message ]



    Re: Why the SEGFAULT in this code?  
Nils O. Selåsdal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-04 02:21 AM

On Fri, 17 Sep 2004 16:06:25 -0700, RJGraham wrote:

> Can someone tell me why the code below should segfault at the line
> indicated?
>
> Also, why does strtok_r() require a **ptrptr and not a *ptr?
Because how else could it change the pointer you supplied ?

> (Maybe this is a clue to my problem and misunderstanding of how strtok_r
> is used...).
>
> Thanks for any insight.
>
> -Randy
>
> PS: str arg is always guaranteed to point to a valid string < 127 chars
>
>
> void getNameValuePairFromString(char * str, string & name, string & value)
> {
You are changing the buf pointer with the calls to
strtok_r, so when you free it, buf doesn't point to
what you malloced.
Do;

char * buffer = (char *) malloc(128);
char *buf = buffer;
>     char * tok = strtok_r(str, " =\n\r", &buf);
>     if (tok != NULL)
>     {
>        name = tok;
>        tok = strtok_r(NULL, " =\n\r", &buf);
>        if (tok != NULL)
>           value = tok;
>     }

>     free(buf); // seg fault here !!!
And;
free(buffer);
> }
You can probably avoid the malloc/free stuff alltogether, perhaps
just initializing buf to NULL suffices. Never used strtok, and my docs
doesn't do a very good job explaining the _r version though..






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:15 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register