initialization of flexible array of struct pointer

2019-09-07 17:28发布

问题:

I'm learning hashtable data structures and I want to make a hashtable with a flexible length array of pointers to struct Link (linked list pieces), so that hashtable initialization will set the array to be a length input into the initialization function.

At first I was getting the error "flexible array not at the end of struct". When its at the end (as shown) the program crashes (but it still compiles). This is my code:

typedef struct Link{
    int key;
    char *name;
    struct Link *next;
} Link;

typedef struct HashTable{
    int numberOfEntries;
    int numberOfBuckets;
    Link *Table[];
} HashTable;

HashTable *hashtableInit(int size){
    HashTable *newHT = malloc(sizeof(HashTable));
        if (newHT != NULL){
            newHT->numberOfEntries = 0;
            newHT->numberOfBuckets = size;
            for (int i = 0; i < newHT->numberOfBuckets; i += 1){
                newHT->Table[i] = NULL;
            }
            return newHT;
        } else {
            printf("Error in memory allocation.\n");
            fflush(stdout);
            return NULL;
        }
    }
}

It works if I set the array to a constant and input the same value into the init function:

#define SIZE 11

typedef struct Link{
    int key;
    char *name;
    struct Link *next;
} Link;

typedef struct HashTable{
    Link *Table[SIZE];        
    int numberOfEntries;
    int numberOfBuckets; 
} HashTable;

HashTable *hashtableInit(int size){ // works if SIZE is passed into function as size parameter
    HashTable *newHT = malloc(sizeof(HashTable));
        if (newHT != NULL){
            newHT->numberOfEntries = 0;
            newHT->numberOfBuckets = size;
            for (int i = 0; i < newHT->numberOfBuckets; i += 1){
                newHT->Table[i] = NULL;
            }
            return newHT;
        } else {
            printf("Error in memory allocation.\n");
            fflush(stdout);
            return NULL;
        }
    }
}

The second code block works perfectly. Any insights would be greatly appreciated. Thanks for your time. Chris

回答1:

You should allocate memory as

HashTable *newHT = malloc(sizeof *newHT + size * sizeof newHT->Table[0]);


回答2:

Your

 HashTable *newHT = malloc(sizeof(HashTable));

is wrong, because no space is given for the flexible array member. Should probably be

 HashTable *newHT = malloc(sizeof(HashTable)+size*sizeof(Link*));