简体   繁体   中英

Segmentation fault in dynamic string array

I have an array of strings that is dynamically sized (I won't know the size of the strings at compile) that keeps giving me a segmentation fault error. The array is contained in a struct called hm and it has an array for the strings as well as an array for values. This part of the code is only to resize the string array properly when a new string is added to the struct. I am relatively new to C and structs, so if there is a better way to implement this I would love to hear about it. I have already tried looking around for this situation and most seem to be having the issue with the outer array using sizeof(char) instead of sizeof(char*) , but when I changed that the issue still happens.

//problematic part of the function
char** t = (char**)realloc(hm->keys, hm->size * sizeof(char*));
if (t) hm->keys = t;
for (i = 0; i < hm->size; i++) {
    char* temp = (char*)realloc(hm->keys[i], hm->largestKey * sizeof(char)); //seg fault here
    if (temp) {
        hm->keys[i] = temp;
    }
}

//struct
typedef struct HM_struct {
    size_t size;
    size_t largestKey;
    char** keys;
    int* values;

    void (*add)(struct HM_struct* hm, char* key, int value);
} HM;

The problem is that when you realloc() and increase the allocated memory size, the new memory is not initialised (or with a debug library, initialised to a sentinal value). So, assuming you know the oldSize , a quick fix is:

char** t = realloc(hm->keys, hm->size * sizeof(char*)); // As before
if (t) hm->keys = t; // As before
for (i = oldSize; i < hm->size; i++)
    hm->keys[i] = NULL;

Now, according to the realloc() definition , when you call:

char* temp = realloc(NULL, hm->largestKey * sizeof(char));

It behaves as:

char* temp = malloc(hm->largestKey * sizeof(char));

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