Andrei Dobre
Andrei Dobre

Reputation: 103

Segmentation fault when char array is declared inside function

Today I experienced a strange behaviour while writing some C/C++ code and I was wondering what the explanation is.

Here are two code snippets showing the same function, the first one has the static array "char comp[BUFFLEN];" declared outside the function while for the second one it is declared inside the function. BUFFLEN was defined with size 1024. The odd behaviour I am referring to is this: the first function runs perfectly and provides the expected output while the second one crushes with a segfault. I know where the mistake is - I did not allocate memory for k before copying (obviously, after fixing, both variants work well), yet I was hoping someone could tell me what happened and why I did't get an error for the first one. Thanks!

char comp[BUFFLEN];
void gen_struct(char* path){
    char pathc[BUFFLEN];
    memset(comp, 0, BUFFLEN);
    comp[0] = '\0';
    strcpy(pathc, path);
    cout << path << endl;
    char* p = strtok(pathc, "/");
    char* k;
    strcpy(k,p);
    do{
        if(p == NULL){
            cout << "Write " << k << " to disk\n";
            break;
        }
        sprintf(comp, "%s/%s", comp, p);
        // strcat(comp, p);
        cout << "Making directory: " << comp << endl;
        // Sysops::mkdir(comp);
        strcpy(k,p);
        p = strtok(NULL, "/");
    }while(true);
}

void gen_struct(char* path){
    char pathc[BUFFLEN];
    char comp[BUFFLEN];
    memset(comp, 0, BUFFLEN);
    comp[0] = '\0';
    strcpy(pathc, path);
    cout << path << endl;
    char* p = strtok(pathc, "/");
    char* k;
    strcpy(k,p);
    do{
        if(p == NULL){
            cout << "Write " << k << " to disk\n";
            break;
        }
        sprintf(comp, "%s/%s", comp, p);
        // strcat(comp, p);
        cout << "Making directory: " << comp << endl;
        // Sysops::mkdir(comp);
        strcpy(k,p);
        p = strtok(NULL, "/");
    }while(true);
}

Sorry if the title is inappropriate, I tried to think of something someone would actually google.

Upvotes: 0

Views: 468

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

Just looking at your code without inspecting in detail shows an evident problem

  1. You don't check if strtok() returned NULL, dereferencing a NULL pointer is undefined behavior, the faulty line is

    char* p = strtok(pathc, "/");
    

    and you strcpy() the "token" right after that.

  2. You don't allocate space for k, so k is not initialized and it contains garbage, trying to write to the "address" it points to, is undefined behavior.

The issue will probably occur as soon as this line

strcpy(k, p)

is reached, it's not possible to predict what the behavior of the program will be, but you can expect it to behave differently when variables are declared/defined at different places, since the program layout will be different and the behavior generally depends on that.

You are also working as if it was a c program, while it's evidently a c++ program, c++ programmers wouldn't work with strings using strtok() and char pointers, c programmers have little to do about it and must work with this things, since you are using a c++ compiler I would suggest std::string and you can split the string, don't need to worry about allocating memory, etc.

Upvotes: 2

Related Questions