0

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

5
  • The second example should have Link *Table[SIZE]; as the last member of struct HashTable ! Commented Nov 3, 2013 at 1:30
  • 1
    On the side: perror("mem"); fprintf(stderr, "Error in memory allocation.\n"); Commented Nov 3, 2013 at 1:31
  • @BasileStarynkevitch it works though Commented Nov 3, 2013 at 1:32
  • Yes, it works, but it is not the same (or nearly the same) as the first. Commented Nov 3, 2013 at 1:33
  • @BasileStarynkevitch how come? Commented Nov 3, 2013 at 1:39

2 Answers 2

3

You should allocate memory as

HashTable *newHT = malloc(sizeof *newHT + size * sizeof newHT->Table[0]);
Sign up to request clarification or add additional context in comments.

Comments

1

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*));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.