strtok() crashing program after function calling it is called more than once

StackOverflow https://stackoverflow.com/questions/21442783

  •  04-10-2022
  •  | 
  •  

سؤال

I'm having a bit of trouble figuring out why strtok() is crashing my program

main()
{
    NodePtr root, cwd;
    char line[128] = {'\0'};
    char command[16] = {'\0'};
    char pathname[64] = {'\0'};
    char dirname[64] = {'\0'};
    char basename[64] = {'\0'};

    root = (NodePtr)malloc(sizeof(Node));

    gets(pathname);

    strcpy(root->name, "/");
    root->nodeType = 'D';
    root->childPtr = NULL;
    root->parentPtr = NULL;
    root->siblingPtr = NULL;

    mkdir(&root, pathname);
    mkdir(&root, "/abc/fa");
}

when I call mkdir the first time, everything works as expected (more specifically using strtok()). But once mkdir gets called the second time, my program crashes when mkdir is called.

    void mkdir(NodePtr *root, char pathname[64])
    {
        char dirname[64] = {'\0'}; //only local variable for dirname
        char basename[64] = {'\0'};  //only local variable for basename
        int i = 0;
        int j = 0;
        int cut = 0;
        int numOfDir = 0; //number of directories
        int len = 0;  //length of entered pathname
        char* tok; //temp value to tokenize string and put it into dirPath

        char** dirPath;  //an array of strings that keeps the pathway needed to take to create the new directory

        NodePtr newNode;
        NodePtr currNode;
        NodePtr currParentNode;

        tok = "\0";

        ........

        printf("tok: %s\n", tok);
        tok = strtok(pathname, "/");   //start first tokenized part
        strcpy(dirPath[i], tok);  //put first tokenized string into dirPathp[]
    //  printf("string: %s\n", dirPath[i]);
        i++;
        while(i < numOfDir)
        {
            tok = strtok(NULL, "/");
            strcpy(dirPath[i], tok); //put tokenized string into array dirPath[]
    //      printf("string: %s\n", dirPath[i]);
            i++;
        }
         ..........

My program specifically breaks at

tok = strtok(pathname, "/"); 

Did strtok keep the input from the first call to mkdir and that's why it's crashing? Pretty new to strtok so im sorry for the confusion. thanks!

هل كانت مفيدة؟

المحلول

You're not using strtok itself wrong, but you are using C string literals wrong.

void mkdir(NodePtr *root, char pathname[64])

This function prototype is equivalent to void mkdir(NodePtr *root, char *pathname) and should be written that way. I mention this because it's important to understand that you are passing strings into mkdir by reference.

mkdir(&root, pathname);
mkdir(&root, "/abc/fa");

On the first call, the pathname argument is set to point to the pathname variable in main, which is in writable memory, so everything works.

On the second call, the pathname variable is set to point to the string literal "/abc/fa", which is in read-only memory, so you crash. You would get the same crash if you did anything in mkdir that attempted to modify the array pointed to by pathname, via a library function or otherwise.

The simplest available cure is to write

char pathname2[64] = "/abc/fa";

in main, and then pass that to mkdir; this causes the compiler to generate a copy from the string literal to another writable character array. More sophisticated approaches are possible, and even advisable, but I'd have to know a lot more about what your larger goals are.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top