简体   繁体   中英

Segmentation fault on calling function more then once

running this function more then once will cause a Segmentation fault and i cannot figure out why. Im not looking for alternative ways to split a string.

SplitX will continue splitting for x ammount of delimiters (be it '|' or '\\0') and return the x or the number of substrings it could make.

I should note i have just restarted coding in C after 3 years of easy JavaScript and PHP so i could be missing something obvious.

int splitX(char **array, char *string, int x) {
    int y;
    int z;
    int index = 0;
    int windex = 0;
    for(y = 0; y < x; y++) {
        z = index;
        while(string[index] != '\0' && string[index] != '|') {
            index++;
        }
        char **tempPtr = realloc(array, (y+1)*sizeof(char *));
        if(tempPtr == NULL) {
            free(array);
            return -3;
        }
        array = tempPtr;
        array[y] = malloc(sizeof(char) * (index - z + 1));
        windex = 0;
        for(; z < index; z++) {
            array[y][windex] = string[z];
            windex++;
        }
        array[y][windex] = '\0';
        if(string[index] == '\0')
            break;
        index++;
    }
    return y+1;
}

int main() {
        char **array;
        int array_len = splitX(array, query, 2);
        printf("%s %s %d\n", array[0], array[1], array_len);
        while(array_len > 0) {
            free(array[array_len-1]);
            array_len--;
        }
        free(array);
        array_len = splitX(array, "1|2\0", 2);
        printf("%s %s %d\n", array[0], array[1], array_len);
        while(array_len > 0) {
            free(array[array_len-1]);
            array_len--;
        }
        free(array);
}
char **array;
int array_len = splitX(array, query, 2);

This lets splitX() use the uninitialized array , which results in undefined behavior.

Furthermore, C has no pass-by-reference - when you write

 array = tempPtr;

inside the function, that has no visible effect outside it.

Im not looking for alternative ways to split a string.

You should really be. Your current approach is at best non-idiomatic, but it also has some other mistakes (like returning y + 1 for some reason where y would do certainly, etc.).

You are also reinventing the wheel: for string and character searching, use strstr() , strchr() and strtok_r() from the C standard library; for duplicaitng a string, use strdup() instead of going through the string manually, etc., etc...

What else:

  • use size_t for sizes instead of int ;

  • maintain const correctness by using const char * for input strings.


char **split(const char *s, size_t *sz)
{
    char **r = NULL;
    size_t n = 0, allocsz = 0;
    const char *p = s, *t = p;
    int end = 0;

    do {
        const char *tmp = strchr(p, '|');
        if (tmp == NULL) {
            p = p + strlen(p);
            end = 1;
        } else {
            p = tmp;
        }

        if (++n > allocsz) {
            if (allocsz == 0)
                allocsz = 4;
            else
                allocsz <<= 1;

            char **tmp = realloc(r, sizeof(*r) * allocsz);
            if (!tmp) abort(); // or whatever, handle error
            r = tmp;
        }

        r[n - 1] = malloc(p - t + 1);
        memcpy(r[n - 1], t, p - t);
        r[n - 1][p - t] = 0;

        p++;
        t = p;
    } while (!end);

    *sz = n;
    return r;
}

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