We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using the code below to initialize buckets for hashmap and frees hashmap:
#include <stdlib.h> typedef struct hashmap_node { char *key; void *val; struct hashmap_node *next; } hashmap_node_t; typedef struct { int size; hashmap_node_t **buckets; } hashmap_t; hashmap_t *hashmap_create(int size) { hashmap_t *map = malloc(sizeof(hashmap_t)); map->size = size; map->buckets = malloc(size * sizeof(hashmap_node_t *)); for (int i = 0; i < map->size; i++) map->buckets[i] = 0; return map; } void hashmap_free(hashmap_t *map) { for (int i = 0; i < map->size; i++) { for (hashmap_node_t *cur = map->buckets[i], *next; cur; cur = next) { next = cur->next; free(cur->key); free(cur->val); free(cur); cur = next; } } free(map->buckets); free(map); } int main() { hashmap_t *map = hashmap_create(16); map->buckets[0] = malloc(sizeof(hashmap_node_t)); // Simulates put at first bucket map->buckets[0]->key = calloc(1, sizeof(char)); map->buckets[0]->key = malloc(sizeof(int)); map->buckets[0]->next = NULL; hashmap_free(map); return 0; }
Compile with gcc will exit normally, while compile with shecc will cause double free, which is abnormal.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Using the code below to initialize buckets for hashmap and frees hashmap:
Compile with gcc will exit normally, while compile with shecc will cause double free, which is abnormal.
The text was updated successfully, but these errors were encountered: