From f15203afb2be9daaca954c70a53426d479f5b577 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 10 Feb 2020 06:47:24 +0100 Subject: [PATCH] Comment a little bit more the examples --- example/ex-array05.c | 10 +++++++--- example/ex-rbtree01.c | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/example/ex-array05.c b/example/ex-array05.c index dec0e68a..7e8f1abb 100644 --- a/example/ex-array05.c +++ b/example/ex-array05.c @@ -4,12 +4,14 @@ /* This example show how to use complex structure with array embedding another library */ -/* This is a trivial library */ +/* This is a typical trivial library with its interface */ typedef struct lib_ext_struct { int id; - // Other data + // Other data may be added } lib_ext_struct; +/* Method to create a duplicate of the object. + The new object is heap allocated */ static lib_ext_struct *lib_ext_struct_Duplicate(const lib_ext_struct *obj) { lib_ext_struct *p = malloc(sizeof(lib_ext_struct)); @@ -18,6 +20,7 @@ static lib_ext_struct *lib_ext_struct_Duplicate(const lib_ext_struct *obj) return p; } +/* Method to delete any object */ static void lib_ext_struct_Delete(lib_ext_struct *obj) { free(obj); @@ -101,11 +104,12 @@ ARRAY_DEF(/* the array prefix */ array_data_node, INIT_SET(API_6(data_node_init_set)), CLEAR(API_2(data_node_clear)))) -/* Define a global array */ +/* Define a global array with the created type */ array_data_node_t global_array; int main(void) { + /* Call the created method */ array_data_node_init(global_array); /* Do stuff... */ array_data_node_clear(global_array); diff --git a/example/ex-rbtree01.c b/example/ex-rbtree01.c index 320ce1e1..8df541d3 100644 --- a/example/ex-rbtree01.c +++ b/example/ex-rbtree01.c @@ -4,6 +4,15 @@ #include "m-rbtree.h" +// Let's define one utility function. +void *xmalloc(size_t n); +void *xmalloc(size_t n) +{ + void *p = malloc (n); + if (!p) abort(); + return p; +} + // Let's define a new type. typedef struct { int key; @@ -12,7 +21,7 @@ typedef struct { #define SIZE 256 -void *xmalloc(size_t n); +// Let's define the prototypes of the methods of this new type. void my_type_init(my_type_t x); void my_type_clear(my_type_t x); void my_type_init_set(my_type_t x, const my_type_t y); @@ -24,13 +33,6 @@ void my_type_out_str(FILE *f, const my_type_t x); bool my_type_in_str(my_type_t x, FILE *f); void my_type_rand(my_type_t x); -void *xmalloc(size_t n) -{ - void *p = malloc (n); - if (!p) abort(); - return p; -} - // Let's define its constructor void my_type_init(my_type_t x) {