5.2 User interface (main.c) Two new commands should now be made available: clr: clear the dictionary. Nothing is printed in return. • siz: print the current size of the dictionary. The only difficulty here is that your previous commands had argument(s), while these two have none. Here is an example run: $ src/dict > put a:b > siz 1 > put a: c > siz 1 > get a > clr > siz 0 > get a Activate Windows Also, now that dict destroy is available, your program is expected to call it when finishing treating the input. Consequently, your program should have no memory leaks. 6 Part 3: dmp, 1df, svf 6.1 Dictionary library (dict.c) In dmp and svf you will have to go through the whole dictionary. At this point, there is no way for main.c to do so; as is the point of most libraries, the implementation details in dict.c are hidden to the user (in our case, main.c), so it is not possible for main.c to go through the list stored in a dict_t. -Note When main.c is compiled, the only information it has about the type dict_t is given by dict.h, since C files are compiled separately before being linked (Ch. 7). When compiling main.c, the compiler only knows about the forward declaration of the type: typedef struct dict dict_t; If you tried dereferencing a variable of type dict_t* in main.c, you would obtain the error: error: invalid use of incomplete typedef 'dict_t' {aka ‘struct dict'} Crucially, the compiler does not know what is the size of an object of type dict_t, since it does not know what are its fields (the full type, with, e.g., next, val, key, is only declared in dict.c). So how come using dict_t* is fine? This is because this is a pointer type, hence its size is just the size of any pointer (64 bits). However, if you were to use pointer arithmetic, this would also fail; with dict++ in main.c, the error would read: main.c: error: increment of pointer to an incomplete type 'dict_t' {aka 'struct dict'} This is because pointer arithmetic depends on the size of the pointed object: int *i = NULL; long *1 = NULL; printf ("%p + 1 == %p, %p + 1 == %p", i, i + 1, 1, 1 + 1); // -> "(nil) + 1 == 0x4, (nil) + 1 == 0x8" Activate Windows
5.2 User interface (main.c) Two new commands should now be made available: clr: clear the dictionary. Nothing is printed in return. • siz: print the current size of the dictionary. The only difficulty here is that your previous commands had argument(s), while these two have none. Here is an example run: $ src/dict > put a:b > siz 1 > put a: c > siz 1 > get a > clr > siz 0 > get a Activate Windows Also, now that dict destroy is available, your program is expected to call it when finishing treating the input. Consequently, your program should have no memory leaks. 6 Part 3: dmp, 1df, svf 6.1 Dictionary library (dict.c) In dmp and svf you will have to go through the whole dictionary. At this point, there is no way for main.c to do so; as is the point of most libraries, the implementation details in dict.c are hidden to the user (in our case, main.c), so it is not possible for main.c to go through the list stored in a dict_t. -Note When main.c is compiled, the only information it has about the type dict_t is given by dict.h, since C files are compiled separately before being linked (Ch. 7). When compiling main.c, the compiler only knows about the forward declaration of the type: typedef struct dict dict_t; If you tried dereferencing a variable of type dict_t* in main.c, you would obtain the error: error: invalid use of incomplete typedef 'dict_t' {aka ‘struct dict'} Crucially, the compiler does not know what is the size of an object of type dict_t, since it does not know what are its fields (the full type, with, e.g., next, val, key, is only declared in dict.c). So how come using dict_t* is fine? This is because this is a pointer type, hence its size is just the size of any pointer (64 bits). However, if you were to use pointer arithmetic, this would also fail; with dict++ in main.c, the error would read: main.c: error: increment of pointer to an incomplete type 'dict_t' {aka 'struct dict'} This is because pointer arithmetic depends on the size of the pointed object: int *i = NULL; long *1 = NULL; printf ("%p + 1 == %p, %p + 1 == %p", i, i + 1, 1, 1 + 1); // -> "(nil) + 1 == 0x4, (nil) + 1 == 0x8" 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