简体   繁体   中英

Segmentation fault in accessing & modifying string (char*) array in C

I've been getting segmentation faults (with gdb printing "??" on backtraces) on a program I'm trying to compile for a while now and after trying many things (such re-programming a data structure I used which should work now) I still kept getting segfaults although now it gave me a line (which I added a comment onto here).

getMains() is ran multiple times to tokenize different lines from the same file.

I wanted mains to be an array of size 4 but when passing it as "char * mains[4]" II got a compile error for trying to pass it an array (*)[4] which I've never dealt with beforehand (Just started using C). I'm assuming maybe that could be a problem if I try to access any part that wasn't used, but the problem happens while initializing the indices of the array.

The code I'm trying to get to work, where the "char *** mains" argument is taking in a &(char **) from a separate function "runner" which I want to be edited so I can look at its contents in "runner":

bool getMains(FILE * file, char *** mains)
{
    char line[256];
    int start = 0;
    char * token;
    const char * mainDelim = "\t \n\0", * commDelim = "\n\t\0";

    if(fgets(line, sizeof(line), file) == NULL)
        return false;

    while(line[0] == '.')
        if(fgets(line, sizeof(line), file) == NULL);
            return false;

    if(line[0] == '\t' || line[0] == ' ')
    {
        (*mains)[0] = " ";
        start = 1;
    }

    token = strtok(line, mainDelim);
    int i;
    for(i = start; token != NULL; ++i)
    {
        (*mains)[i] = strdup(token); // <- gdb: Segmentation Fault occurs here

        if(i % 3 == 2)
            token = strtok(NULL, commDelim);
        else
            token = strtok(NULL, mainDelim);
     }

     free(token); // Unsure if this was necessary but added in case.
     return true;
}


/* Snippet of code running it... */
void runner(FILE * file) {
    char ** mains;
    if(!getMains(*file, &mains))
        return;
    while(strcmp(mains[1], "END") != 0){
        /* do stuff lookinig through indices 0, 1, 2, & 3 */
        if(!getMains(*file, &mains))
            break;
    }
}

Any tips on this or just generally safely modifying arrays through other functions?

Should I change getMains() into "getMains(FILE * file, char ** mains[4]);" and pass it a &"char * mains[4]") for it to be a set size as wanted? Or would that also produce errors?

You need to allocate memory for mains, it should look like this:

char ** mains;
mains = malloc(some number N * sizeof(char*));

You need something like this if you don't use strdup, which allocates the memory for you:

for (int i = 0; i < N; ++i) {
  mains[i] = malloc(some number K);
}

In all cases, do not forget to call free on every pointer you received from malloc or strdup . You can skip this part if the program ends right after you would call free .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM