Question:
strtok() in c programming causes segmentation fault?
lozter
2010-03-14 05:54:12 UTC
I have a function

char *element (char *stringss)
{
//code
char *pch = (char *) malloc (200*sizeof(char));
pch = strtok (stringss,","); - seg fault here
//code
}

Im i using strtok wrong???
Four answers:
Anil
2010-03-14 06:04:39 UTC
The call to strtok() looks correct. I can only see the reason for a segmentation fault to be the NULL pointer in string 'stringss' variable.



Just verify that the pointer stringss has correct value and is not NULL.

In fact, as a good programming practice, you should always check for the NULL pointer before accessing its value like this:

char *element (char *stringss)

{

if ( stringss != NULL)

{

//code

char *pch = (char *) malloc (200*sizeof(char));

if (pch != NULL)

{

pch = strtok (stringss,","); - seg fault here

}

}

//code

}
cja
2010-03-14 13:15:17 UTC
I do think you have some misconceptions about how strtok works. For one thing, it returns a pointer to char, and for some reason you're allocating 200 bytes for the pointer returned from your strtok call. It's almost as if you're intending for strtok to copy the token into your buffer, which is not what happens. If you want your element function to tokenize stringss, you need to do something like this:



#include

#include



#define MAX_LINE_LEN 1024

#define WS " ,"

#define N 2



char *element (char *);

char *S[] = { "one, two, three",

                    "four, five, six" };



int main(int argc, char *argv[]) {

    int i;

    char *p;



    for (i = 0; i < N; i++) {

        do {

            if ((p = element(S[i])) != NULL) {

                printf("%s\n",p);

            }

        } while (p != NULL);

    }

    return 0;

}



char *element (char *stringss) {

    static char *tok = NULL;



    if (tok == NULL) {

        tok = strtok(stringss,WS);

    } else {

        tok = strtok(NULL,WS);

    }

    return tok;

}





#if 0



Program output:



one

two

three

four

five

six



#endif
bernmeister
2010-03-14 07:32:22 UTC
The problem is the memory used for stringss. This problem is in the caller of element(). If you do something like this, it'll cause a crash:



void main(void)

{

char *random_ptr;

(void)element(random_ptr);

}



Why? No memory set aside, just a pointer. Do this instead:



void main(void)

{

char better_ptr[] = "some string text";

(void)element(better_ptr);

}
?
2016-12-15 01:54:34 UTC
Segmentation fault implies you have achieved some thing incorrect including your memory administration. Are you applying Linux (or comparable)? i think of you're, as you communicate with it as a segmentation fault. if so, look at applying Valgrind, which will spotlight which strains are inflicting unusual behaviour, in case you collect with -g in gcc, then execute your application with: $ valgrind ./programname It sounds like your code ought to be high-quality (as quickly as you have swapped the %s with %c) so i'm curious to nicely referred to as to why this could take place. one element it particularly is particularly useful to video demonstrate out for is that 'length' as you have defined it particularly is an int, while sizeof() returns a size_t (whether this shouldn't rely), and in C (opposite to C++), you do no longer could desire to forged the return of malloc. the respond above with regard to the nul-terminated string will reason issues because it writes previous the tip of para (4th index), and it is going to no longer be mandatory as you're applying it as an array, no longer a string.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...