Add sample for string map

master
En Yi 2024-12-30 22:44:06 +08:00
parent 690e705c94
commit e70baef781
1 changed files with 24 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include "base.h"
#include "memory_arena.h"
#include <assert.h>
int main(void) {
@ -27,6 +28,29 @@ int main(void) {
mem_arena_print();
}
{
cc_map(char*, int) str_map;
cc_init(&str_map);
cc_reserve(&str_map, 4);
mem_arena_print();
{
// For char*, you have to allocated memory (static or dynamic)
char* sample = mem_arena_malloc(4);
memcpy(sample, "lol", 4);
cc_insert(&str_map, sample, 15);
}
int* val = cc_get(&str_map, "lol");
printf("str key get: %p\n", val);
mem_arena_print();
// Need to retrieve char* to free, if it is dynamic
// On retrive, dereference to get point and free
// Safer to NULL check first
// but we know it exists
mem_arena_free((void*)*cc_key_for(&str_map, val));
cc_cleanup(&str_map);
mem_arena_print();
}
{
printf("Map of maps\n");
cc_map(int, cc_map(int,int)) map_of_map;