From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: Newbie - Perl Equivalent Split - Seg Faults
Date: Mon, 13 Dec 2004 21:21:51 +0100 [thread overview]
Message-ID: <20041213202151.GF16958@lug-owl.de> (raw)
In-Reply-To: <BDE333E9.1622%dsessions@ionosphere.net>
[-- Attachment #1: Type: text/plain, Size: 2710 bytes --]
On Mon, 2004-12-13 11:56:25 -0500, Darren Sessions <dsessions@ionosphere.net>
wrote in message <BDE333E9.1622%dsessions@ionosphere.net>:
> #include <stdio.h>
> #include <string.h>
>
> char *split_char(char *string, char *delim) {
> fprintf( stderr, "\tString = %s \n", string);
> fprintf( stderr, "\tDelimiter = %s \n", delim);
> string = strtok(string, delim);
Rule of thumb: don't use strtok(), because it internally maintains some
parsing state. Even if you (right now) only have a single-threaded
program which wouldn't probably suffer from strtok()'s limitations, you
don't know if your code won't be--at some time--used in a threaded
environment. Just use strtok_r() instead. ...and even if you use the
strtok_r(), try to avoid it:-)
Both strtok_r() and strtok() modify the string you supplied. While this
is acceptable for some uses, it'll break from time to time (as in this
example). This is why the man page actually warns about using these
functions.
...but what happens here, why does it break? Well, that's easy. Keeping
in mind that this function actually modifies the supplied string, this
is actually where it segfaults...
> return string;
> }
>
> int main()
> {
> char *testvar;
> testvar = split_char("test-hello", "-");
...and it segfaults because you supply "test-hello right here, right the
way you do it.
If you put some "sdfkjhsdf constant somewhere, of if you have a
char *string = "some text";
the compiler is allowed to imply that these strings are never ever
modified, but (in the 2nd example) the *pointer* to the string may
change. So gcc knows that "test-hello" won't ever be modified and puts
it into a segment of memory that gets configured as "modify forbidden".
When split_char() calls strtok(), the later one tries to modify the
string (replaces ' ' by '\0') which will result in a segmentation
violation. So you need to force the compiler into laying out your text
as a modifyable string. You can do this by:
char test_hello[] = "test_hello";
...
testvar = split_char (test_hello, "-");
...
Notice that this time, I didn't declare a pointer to string (char *),
but an array of chars (char []). This builds up to the difference of
"modify forbidden" versus "modify allowed".
> fprintf( stderr, "\tArray = %s \n", testvar);
> return(0);
> }
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
next prev parent reply other threads:[~2004-12-13 20:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-12-13 16:56 Newbie - Perl Equivalent Split - Seg Faults Darren Sessions
2004-12-13 17:10 ` Darren Sessions
2004-12-13 20:21 ` Jan-Benedict Glaw [this message]
-- strict thread matches above, loose matches on Subject: below --
2004-12-13 21:33 Huber, George K RDECOM CERDEC STCD SRI
2004-11-16 21:20 ` J.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20041213202151.GF16958@lug-owl.de \
--to=jbglaw@lug-owl.de \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).