1

Assume that a HashEntry is a struct that has a pointer to a list(head). Bucket of a HashTable has a pointer to Bucket Entries(array of entries) Bucket Entry struct has a key that is char*.

struct BucketEntry{
    char* key;
    //irrelevant data
};

struct Bucket{
    BucketEntry* entries;
    struct Bucket* next;
    int counter; // counts how keys are in.
};

struct HashEntry{
    struct Bucket* head;
};

A hash table is a pointer of type HashEntry* to an array of entries, and heads are initialized to NULL

struct HashEntry* table = malloc(buckets*sizeof(struct HashEntry));
for(i = 0; i < buckets; i++){
    table[i].head = NULL;
}

After that, i create a bucket like this and i assign it in to the proper hash ceil:

struct Bucket* bucket = malloc(sizeof( struct Bucket));
table[hash].head = bucket;

What is the proper way to allocate an array of Bucket Entries of quantum x? After the allocation , what is the proper way to assign it to the proper bucket&Hash ceil ?

Thank you and sorry for my english.

ryyker
  • 22,849
  • 3
  • 43
  • 87
TuMama
  • 307
  • 1
  • 2
  • 8
  • 1
    In all the struct constructs shown, you are using the exact same symbol for both the `typedef` and the` struct` `name`. For example, you use `typedef struct BucketEntry {`, then at the end use `}BucketEntry;`. By doing so, one mask's the other, and is confusing. Perhaps choose a unique symbol for each. – ryyker Mar 13 '20 at 12:51
  • Ok , i am removing the typedef then – TuMama Mar 13 '20 at 12:53
  • 1
    There is nothing absolutely wrong with the memory allocation statements, but I am unsure what you are doing with some of them. Is your goal here to create and use [linked lists](https://www.geeksforgeeks.org/linked-list-set-1-introduction/)? – ryyker Mar 13 '20 at 12:58
  • Yes. This is my goal. But i want to dynamicaly allocate some space of x in the node. Ιn this case, Bucket represent the linked list node. – TuMama Mar 13 '20 at 13:01
  • `struct Bucket* bucket = malloc(sizeof( *bucket)*x);` – ryyker Mar 13 '20 at 13:02
  • sizeof( pointer to struct bucket) ? – TuMama Mar 13 '20 at 13:04
  • @TuMama `sizeof( *bucket)` is correct, it means "the size of the thing `bucket` points to. You could also write `sizeof(struct bucket)`, but `sizeof(*bucket)` is more idiomatic – Jabberwocky Mar 13 '20 at 13:11
  • Pardon me, i want to allocate dynamicaly space for **X** **BUCKET_ENTRIES** in the node. In our case **Node is the bucket**. So the allocation should be like this `table[hash].head->entries = malloc(x*sizeof(Bucket_Entries));` – TuMama Mar 13 '20 at 13:13
  • @TuMama there is no `Bucket_Entries` symbol in your question – Jabberwocky Mar 13 '20 at 13:17
  • @Jabberwocky **BucketEntry** ,pardon me again. `table[hash].head->entries = malloc(x*sizeof(BucketEntry))`; – TuMama Mar 13 '20 at 13:18
  • @TuMama - `struct Bucket`, `*bucket` are both the same size. As Jabberwocky has stated, one is more typically used (idiomatic), and one is not. Finally, regarding your latest comment, because its not a `typedef`, `BucketEntry` needs to be `struct BucketEntry`. This [question/answer](https://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions) may be a good read for you! – ryyker Mar 13 '20 at 14:33
  • `bucket->entries = malloc(sizeof(struct BucketEntry) * x);`? – Ian Abbott Mar 13 '20 at 15:34
  • Ok thank you for your time, you were enlightening ! – TuMama Mar 13 '20 at 15:45
  • ...One more note about posting a good question. Once you've posted, its okay to edit the post to make _clarifications_ in response to comment requests, or _improve formatting_, etc. But almost without exception it is not recommended that you actually change the core elements of your question that are central to the question/problem in the first place. If you do, then any work or suggestions that have been offered to address those issues (that have now been edited away) become useless and confusing for future readers of the post.... – ryyker Mar 13 '20 at 18:55
  • ...Additionally, the the question becomes a moving target for those engaged in attempting to answer. (It is for these reasons I have rolled it back to its original state.) Other than that, this is a decent question! (+1) – ryyker Mar 13 '20 at 19:04
  • @ryyker Thank you for your recommendations. But i've changed only the word " ceil" to " cell" . I've made a typo. – TuMama Mar 14 '20 at 10:53

0 Answers0