Mail Archives: cygwin/2000/06/22/11:42:45
> > I noted that, while using strtok() function, I receive a SIGSEGV
> > which causes a stack dump under NT.
> >
> > The little program I used to understand this behaviour was
> > the following:
> >
> > #include <stdio.h>
> > #include <string.h>
> > #include <stdlib.h>
> >
> > int
> > main()
> > {
> > static char *string = "MARCO#PETTINELLA";
> > static char *separator = "#";
> > char *name;
> > char *surname;
> >
> > name = NULL;
> > surname = NULL;
> >
> > name = strtok (string, separator);
...
> A couple of thoughts:
> 1) The literal 'string' ought to be in a read-only data area.
> So the sigsegv could come from strtok trying to write a 0 over the first
> hash.
> Not all compilers/OS's support such a concept, so it may not show up
> in different implementations.
Yes, it almost certainly is that the string is being placed into read-only
as it should unless you give gcc the -fwritable-strings option. Note, that
a small change to this program can make it very portable:
Change:
static char *string =
to:
static char string[] = "MARCO#PETTINELLA";
Then, string will not be a pointer to the read-only string "MARCO#PETTINELLA",
but will be a 17 character array initialized to MARCO#PETTINELLA\0. Since
it is then initialized data instead of a string constant, it will be
writable in all implementations.
marcus
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com
- Raw text -