Skip to contentSkip to conversation

Newbie - Perl Equivalent Split - Seg Faults

DISCUSSION Darren Sessions 2 participants 3 messages Dec 13, 2004

lore
Share & tools
lore
#0 lore raw

Here is a super simple program that (trys) to split a charvar based on a delimiter. I get no compile errors. If I remove the strtok line, then split_var returns the string passed to it from main just fine.

I tried changing the char *delim from *delim to delim[50] - same problem.

This is something stupid, and probably super simple. Coming from the Perl world, I'm trying to write some equivalent string manipulation functions that I can use throughout my programs to avoid repetition and make the code cleaner and easier to read.

Thanks in advance,

- Darren

#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);
return string;
}

int main()
{
char *testvar;
testvar = split_char("test-hello", "-");
fprintf( stderr, "\tArray = %s \n", testvar);
return(0);
}

#1 lore raw

Oh.. I almost forgot, if I place the strtok in main instead of calling it as a function in split_char - it works.

Thanks,

- Darren

Quoted 52 lines · Darren Sessions

Here is a super simple program that (trys) to split a charvar based on a delimiter. I get no compile errors. If I remove the strtok line, then split_var returns the string passed to it from main just fine.

I tried changing the char *delim from *delim to delim[50] - same problem.

This is something stupid, and probably super simple. Coming from the Perl world, I'm trying to write some equivalent string manipulation functions that I can use throughout my programs to avoid repetition and make the code cleaner and easier to read.

Thanks in advance,

- Darren

#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);
return string;
}

int main()
{
char *testvar;
testvar = split_char("test-hello", "-");
fprintf( stderr, "\tArray = %s \n", testvar);
return(0);
}

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to Darren Sessions

On Mon, 2004-12-13 11:56:25 -0500, Darren Sessions <[email protected]> wrote in message <BDE333E9.1622%[email protected]>:

Quoted 7 lines

#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...

Quoted 7 lines

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".

Quoted 3 lines

fprintf( stderr, "\tArray = %s \n", testvar);
return(0);
}

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +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));