简体   繁体   中英

Weird C segmentation fault

So I have this bit of code

int main(int argc, char *argv[]) {
    char *vendas[1];
    int size = 1;
    int current = 0;
    char buffer[50];
    char *token;
    FILE *fp = fopen("Vendas_1M.txt", "r");

    while(fgets(buffer, 50, fp)) {
        token = strtok(buffer, "\n");
        if (size == current) {
            *vendas = realloc(*vendas, sizeof(vendas[0]) * size * 2);
            size *= 2;
        }
        vendas[current] = strdup(token);
        printf("%d - %d - %s\n", current, size, vendas[current]);
        current++;
    }
}

Here's the thing... Using GDB it's giving a segmentation fault on

vendas[current] = strdup(token);

but the weirdest thing is it works up until the size it at 1024 . The size grows up to 1024 and then it just spits a segmentation fault at around the 1200 element. I know the problem is on the memory reallocation, because it worked when I had a static array. Just can't figure out what.

You cannot reallocate a local array, you want vendas to be a pointer to an allocated array of pointers: char **vendas = NULL; .

You should also include the proper header files and check for fopen() and realloc() failure.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

void free_array(char **array, size_t count) {
    while (count > 0) {
        free(array[--count]);
    }
    free(array);
}

int main(int argc, char *argv[]) {
    char buffer[50];
    char **vendas = NULL;
    size_t size = 0;
    size_t current = 0;
    char *token;
    FILE *fp;

    fp = fopen("Vendas_1M.txt", "r");
    if (fp == NULL) {
        printf("cannot open file Vendas_1M.txt\n");
        return 1;
    }
    while (fgets(buffer, sizeof buffer, fp)) {
        token = strtok(buffer, "\n");
        if (current >= size) {
            char **savep = vendas;
            size = (size == 0) ? 4 : size * 2;
            vendas = realloc(vendas, sizeof(*vendas) * size);
            if (vendas == NULL) {
                printf("allocation failure\n");
                free_array(savep, current);
                return 1;
            }
        }
        vendas[current] = strdup(token);
        if (vendas[current] == NULL) {
            printf("allocation failure\n");
            free_array(vendas, current);
            return 1;
        }
        printf("%d - %d - %s\n", current, size, vendas[current]);
        current++;
    }
    /* ... */
    /* free allocated memory (for cleanliness) */
    free_array(vendas, current);
    return 0;
}    

You only have room for one (1) pointer in you array of char *vendas[1] . So second time around you are outside the limits of the array and are in undefined behavior land.

Also, the first call to realloc passes in a pointer that was not allocated by malloc so there is another undefined behavior.

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