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?  
Rich Gibbs


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


 
09-23-04 02:21 AM

M=E5ns Rullg=E5rd said the following, on 09/17/04 19:39:
> RJGraham <null@null.com> writes:
>=20
>=20 

In addition to what M=E5ns has told you, you also need to insert here:

#include <stdlib.h>[vbcol=seagreen] 
ue)[vbcol=seagreen] 
>=20
>=20

And you should not cast the returned value of malloc, but you should=20
check that it is not NULL.  The returned value from malloc is of type=20
"pointer to void", which can be converted into any object pointer.

If (because stdlib.h is not #included) there is no prototype in scope=20
for malloc, it is assumed to return 'int' by default.  Although it is=20
true on many platforms that you can go between 'int' and pointer types=20
without trouble, it is _not_ always true (and certainly is not=20
guaranteed in standard C).  The presence of the cast will usually=20
prevent the compiler from warning you of a potential problem.

> 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().
>=20
>=20 
>=20
>=20


--=20
Rich Gibbs
rgibbs@alumni.princeton.edu






[ Post a follow-up to this message ]



    Re: Why the SEGFAULT in this code?  
Lev Walkin


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


 
09-23-04 02:21 AM

Rich Gibbs wrote:
>
> And you should not cast the returned value of malloc, but you should
> check that it is not NULL.  The returned value from malloc is of type
> "pointer to void", which can be converted into any object pointer.
>
> If (because stdlib.h is not #included) there is no prototype in scope
> for malloc, it is assumed to return 'int' by default.  Although it is
> true on many platforms that you can go between 'int' and pointer types
> without trouble, it is _not_ always true (and certainly is not
> guaranteed in standard C).  The presence of the cast will usually
> prevent the compiler from warning you of a potential problem.

That's the common thing written in many books and FAQs describing the C
programming language.

However, it is not quite so in case of C++, because of stricter type checkin
g.
It just doesn't allow assigning uncasted malloc() to types different from
void*.

The compatible approach to make the C source code be compilable as
part of C++ framework is to cast the lvalue:

type_t *ptr;
(void *)ptr = malloc(...);

This would prevent C++ complaints and also preserve the desired
"fail when malloc is not declared" semantics.

--
Lev Walkin
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: Why the SEGFAULT in this code?  
Måns Rullgård


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


 
09-23-04 02:21 AM

Lev Walkin <vlm@lionet.info> writes:

> Rich Gibbs wrote: 
>
> That's the common thing written in many books and FAQs describing the C
> programming language.
>
> However, it is not quite so in case of C++, because of stricter type
> checking.  It just doesn't allow assigning uncasted malloc() to
> types different from void*.
>
> The compatible approach to make the C source code be compilable as
> part of C++ framework is to cast the lvalue:
>
> 	type_t *ptr;
> 	(void *)ptr = malloc(...);
>
> This would prevent C++ complaints and also preserve the desired
> "fail when malloc is not declared" semantics.

Using a cast expression as an lvalue is non-standard and doesn't work
with the most recent gcc versions.

--
Måns Rullgård
mru@mru.ath.cx





[ Post a follow-up to this message ]



    Re: Why the SEGFAULT in this code?  
Floyd L. Davidson


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


 
09-23-04 02:21 AM

Lev Walkin <vlm@lionet.info> wrote:
>Rich Gibbs wrote: 
>
>That's the common thing written in many books and FAQs describing the C
>programming language.
>
>However, it is not quite so in case of C++, because of stricter type checki
ng.
>It just doesn't allow assigning uncasted malloc() to types different from
>void*.
>
>The compatible approach to make the C source code be compilable as
>part of C++ framework is to cast the lvalue:
>
>       type_t *ptr;
>       (void *)ptr = malloc(...);
>
>This would prevent C++ complaints and also preserve the desired
>"fail when malloc is not declared" semantics.

However, if that was in fact C++ code, there is no reason for using
malloc() to obtain memory space.

Of course, in that particular example, the is no reason to malloc()
space for the pointer in either C or C++!  Both the call to malloc()
and free() should be removed.

--
FloydL. Davidson           <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         floyd@barrow.com





[ 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

"Rich Gibbs" <rgibbs@REMOVEalumni.CAPSprinceton.edu> wrote in message
news:414bb388@news101.his.com...
Måns Rullgård said the following, on 09/17/04 19:39:[vbcol=seagreen]
> RJGraham <null@null.com> writes:
>
> 

In addition to what Måns has told you, you also need to insert here:

#include <stdlib.h>[vbcol=seagreen] 

FWIW, this is just a code snippet.
#include <stdlib.h> *is* included in the actual source file, sorry I wasn't
clear on this.

-Randy







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:46 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