5.1.2 dict destroy This is now the function that frees (destroys) a dictionary: void dict destroy (dict_t* dict); This operates just as dict_clear, but in addition should free the memory that was allocated during dict_create. After a call to this function, if any other library function receives the pointer dict, the behavior is undefined (most likely, it will crash). 5.1.3 dict size -Note You may be tempted to set dict to NULL at the end of that function, hoping that this will make it clear to the user that the pointer is now invalid. However, changing the value of dict in dict_destroy will have no impact on the value of dict outside of the function. This is because in C, arguments are always passed by value, that is, the value of the argument is copied when making the call. This is also why we never pass a struct as argument to a function: we want to avoid copying the whole structure when making the call. Additionally, passing a pointer to a struct allows the called function to modify the fields in a way that the caller will see. This simple function returns the current size of the dictionary: size_t dict size (const dict_t* dict); -Note The type size_t is a standard alias for unsigned long, that is, a 64-bit unsigned integer type. It is defined in the header file stddef.h, which is included by most standard headers (e.g., unistd.h, stdio.h,...). 4.1.2 dict_create Equipped with these structures, start by implementing: dict_t* dict_create (); This function should allocate a new structure of type dict_t (aka struct dict), initialize its members, and return the newly allocated structure. To do so, you will call malloc to allocate a sufficient amount of memory: dict t* ret malloc (sizeof (dict_t)); Then, you should initialize the fields of ret. Since ret is a pointer, to access the object it points to, you should use *ret; hence the size field is accessed using (*ret).size. C has a shorthand notation for this very common construct: ret->size = 0; Now make sure to initialize head (since its value is garbage as it was just allocated) and you are done and can return ret. You will be implementing dict_destroy in Part 2; this is the function that will be in charge of freeing the memory that was just allocated: in C, memory that is malloc'd is never automatically freed. Memory allocation is the topic of Ch. 9. 4.1.3 dict_get Next, you will implement: char* dict_get (const dict_t" dict, const char* key); This function goes through the list given by dict. If you use the above structure, this means starting at el = dict->head and checking each time whether the key at el is key; if it is not, we set el el->next, until either key is found, or we reach el ■■ NULL. Activate Windows
5.1.2 dict destroy This is now the function that frees (destroys) a dictionary: void dict destroy (dict_t* dict); This operates just as dict_clear, but in addition should free the memory that was allocated during dict_create. After a call to this function, if any other library function receives the pointer dict, the behavior is undefined (most likely, it will crash). 5.1.3 dict size -Note You may be tempted to set dict to NULL at the end of that function, hoping that this will make it clear to the user that the pointer is now invalid. However, changing the value of dict in dict_destroy will have no impact on the value of dict outside of the function. This is because in C, arguments are always passed by value, that is, the value of the argument is copied when making the call. This is also why we never pass a struct as argument to a function: we want to avoid copying the whole structure when making the call. Additionally, passing a pointer to a struct allows the called function to modify the fields in a way that the caller will see. This simple function returns the current size of the dictionary: size_t dict size (const dict_t* dict); -Note The type size_t is a standard alias for unsigned long, that is, a 64-bit unsigned integer type. It is defined in the header file stddef.h, which is included by most standard headers (e.g., unistd.h, stdio.h,...). 4.1.2 dict_create Equipped with these structures, start by implementing: dict_t* dict_create (); This function should allocate a new structure of type dict_t (aka struct dict), initialize its members, and return the newly allocated structure. To do so, you will call malloc to allocate a sufficient amount of memory: dict t* ret malloc (sizeof (dict_t)); Then, you should initialize the fields of ret. Since ret is a pointer, to access the object it points to, you should use *ret; hence the size field is accessed using (*ret).size. C has a shorthand notation for this very common construct: ret->size = 0; Now make sure to initialize head (since its value is garbage as it was just allocated) and you are done and can return ret. You will be implementing dict_destroy in Part 2; this is the function that will be in charge of freeing the memory that was just allocated: in C, memory that is malloc'd is never automatically freed. Memory allocation is the topic of Ch. 9. 4.1.3 dict_get Next, you will implement: char* dict_get (const dict_t" dict, const char* key); This function goes through the list given by dict. If you use the above structure, this means starting at el = dict->head and checking each time whether the key at el is key; if it is not, we set el el->next, until either key is found, or we reach el ■■ NULL. Activate Windows
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps