* strtok_r crashing
@ 2002-11-14 12:46 Hemant Mohan
2002-11-14 12:50 ` Jan-Benedict Glaw
0 siblings, 1 reply; 5+ messages in thread
From: Hemant Mohan @ 2002-11-14 12:46 UTC (permalink / raw)
To: linux-c-programming
Hi,
I am trying to parse a string using strtok_r (I need reentrant code) as
follows:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str = NULL;
char *temp = NULL;
char **ptr = NULL; // Note that this is a char**
int x = 0;
int y = 0;
str = malloc (10*sizeof(char));
sprintf ( str,"%s", "320x240");
temp = strtok_r ( str, "Xx", ptr );
x = atoi(temp);
temp = strtok_r ( NULL, "Xx", ptr);
y = atoi(temp);
printf("x = %d\t y= %d\n", x, y);
return 0;
}
This code receives a SIGSEGV in strtok_r.
However, if I change the declaration of ptr from char** to char* and change
the third arguement of strtok_r to &ptr it works.
That is
char* ptr = NULL;
temp = strtok_r ( str, "Xx" &ptr);
Can anyone please help me know the difference between these two calls
as I think both calls are similar.
Thanks,
Hemant Mohan
-------------------------------------------------------
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: strtok_r crashing
2002-11-14 12:46 strtok_r crashing Hemant Mohan
@ 2002-11-14 12:50 ` Jan-Benedict Glaw
2002-11-15 5:45 ` Hemant Mohan
0 siblings, 1 reply; 5+ messages in thread
From: Jan-Benedict Glaw @ 2002-11-14 12:50 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 852 bytes --]
On Thu, 2002-11-14 18:16:04 +0530, Hemant Mohan <hemantm@pune.tcs.co.in>
wrote in message <OF8E547BE6.AC22B859-ON65256C71.0045BBB6@pune.tcs.co.in>:
> Hi,
>
> I am trying to parse a string using strtok_r (I need reentrant code) as
> follows:
>
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> char *str = NULL;
> char *temp = NULL;
> char **ptr = NULL; // Note that this is a char**
Note that there's no memory allocated for *ptr.
> This code receives a SIGSEGV in strtok_r.
Surely. Accessing random memory.
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur
fuer einen Freien Staat voll Freier Bürger" | im Internet!
Shell Script APT-Proxy: http://lug-owl.de/~jbglaw/software/ap2/
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: strtok_r crashing
2002-11-14 12:50 ` Jan-Benedict Glaw
@ 2002-11-15 5:45 ` Hemant Mohan
2002-11-15 6:26 ` Glynn Clements
0 siblings, 1 reply; 5+ messages in thread
From: Hemant Mohan @ 2002-11-15 5:45 UTC (permalink / raw)
To: linux-c-programming-owner, linux-c-programming
Does that mean thaat I need to malloc the ptr before passing it to the
strtok_r. If so then what sould be the size ?? should it be the size of the
str (strlen(str))??
On Thursday 14 November 2002 06:20 pm,
linux-c-programming-owner@vger.kernel.org wrote:
> On Thu, 2002-11-14 18:16:04 +0530, Hemant Mohan <hemantm@pune.tcs.co.in>
>
> wrote in message <OF8E547BE6.AC22B859-ON65256C71.0045BBB6@pune.tcs.co.in>:
> > Hi,
> >
> > I am trying to parse a string using strtok_r (I need reentrant code) as
> > follows:
> >
> > #include <string.h>
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main()
> > {
> > char *str = NULL;
> > char *temp = NULL;
> > char **ptr = NULL; // Note that this is a char**
>
> Note that there's no memory allocated for *ptr.
>
> > This code receives a SIGSEGV in strtok_r.
>
> Surely. Accessing random memory.
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: strtok_r crashing
2002-11-15 5:45 ` Hemant Mohan
@ 2002-11-15 6:26 ` Glynn Clements
2002-11-15 6:42 ` Hemant Mohan
0 siblings, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2002-11-15 6:26 UTC (permalink / raw)
To: hemantm; +Cc: linux-c-programming-owner, linux-c-programming
Hemant Mohan wrote:
> > > I am trying to parse a string using strtok_r (I need reentrant code) as
> > > follows:
> > >
> > > #include <string.h>
> > > #include <stdio.h>
> > > #include <stdlib.h>
> > >
> > > int main()
> > > {
> > > char *str = NULL;
> > > char *temp = NULL;
> > > char **ptr = NULL; // Note that this is a char**
> >
> > Note that there's no memory allocated for *ptr.
> >
> > > This code receives a SIGSEGV in strtok_r.
> >
> > Surely. Accessing random memory.
>
> Does that mean thaat I need to malloc the ptr before passing it to the
> strtok_r. If so then what sould be the size ?? should it be the size of the
> str (strlen(str))??
No, you don't need to use malloc(). You just need to ensure that the
third argument points to valid memory, e.g.
char *temp;
char *ptr; /* this is NOT a char ** */
...
temp = strtok_r ( str, "Xx", &ptr );
x = atoi(temp);
temp = strtok_r ( NULL, "Xx", &ptr);
y = atoi(temp);
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: strtok_r crashing
2002-11-15 6:26 ` Glynn Clements
@ 2002-11-15 6:42 ` Hemant Mohan
0 siblings, 0 replies; 5+ messages in thread
From: Hemant Mohan @ 2002-11-15 6:42 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux-c-programming-owner, linux-c-programming
OK, I think I got the point.
Thanks a lot
Hemant
On Friday 15 November 2002 11:56 am, Glynn Clements wrote:
> Hemant Mohan wrote:
> > > > I am trying to parse a string using strtok_r (I need reentrant code)
>
> as
>
> > > > follows:
> > > >
> > > > #include <string.h>
> > > > #include <stdio.h>
> > > > #include <stdlib.h>
> > > >
> > > > int main()
> > > > {
> > > > char *str = NULL;
> > > > char *temp = NULL;
> > > > char **ptr = NULL; // Note that this is a char**
> > >
> > > Note that there's no memory allocated for *ptr.
> > >
> > > > This code receives a SIGSEGV in strtok_r.
> > >
> > > Surely. Accessing random memory.
> >
> > Does that mean thaat I need to malloc the ptr before passing it to the
> > strtok_r. If so then what sould be the size ?? should it be the size of
>
> the
>
> > str (strlen(str))??
>
> No, you don't need to use malloc(). You just need to ensure that the
> third argument points to valid memory, e.g.
>
> char *temp;
> char *ptr; /* this is NOT a char ** */
> ...
> temp = strtok_r ( str, "Xx", &ptr );
> x = atoi(temp);
> temp = strtok_r ( NULL, "Xx", &ptr);
> y = atoi(temp);
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-11-15 6:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-11-14 12:46 strtok_r crashing Hemant Mohan
2002-11-14 12:50 ` Jan-Benedict Glaw
2002-11-15 5:45 ` Hemant Mohan
2002-11-15 6:26 ` Glynn Clements
2002-11-15 6:42 ` Hemant Mohan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).