Skip to content
New issue

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

嵌套自定义类型 #28

Open
activesys opened this issue Jul 22, 2013 · 1 comment
Open

嵌套自定义类型 #28

activesys opened this issue Jul 22, 2013 · 1 comment

Comments

@activesys
Copy link
Owner

当用户使用了嵌套自定义类型的时候,即如下的形式:
typedef struct {
vector_t* pvec; /* pvec = create_vector(abc_t); */
} abc_t;
这个类型注册的时候没有问题,但是当外层容器初始化的时候会导致无限递归调用,直至调用栈满,然后进程崩溃。
例子代码:

  1 #include <stdio.h>
  2 #include <cstl/chash_map.h>
  3 #include <cstl/chash_set.h>
  4 
  5 typedef struct {
  6     hash_set_t* vars;
  7     hash_map_t* field_map;
  8     int status;
  9 } Alias;
 10 
 11 static void _alias_init(const void* cpv_input, void* pv_output)
 12 {
 13     Alias* p = (Alias*)cpv_input;
 14 
 15     p->vars = create_hash_set(char*);
 16     hash_set_init(p->vars);
 17     p->field_map = create_hash_map(char*, Alias); /* You must not be invoke init function */
 18     hash_map_init(p->field_map);
 19     p->status = 0;
 20 
 21     *(bool_t*)pv_output = true;
 22 }
 23 static void _alias_destroy(const void* cpv_input, void* pv_output)
 24 {
 25     Alias* p = (Alias*)cpv_input;
 26 
 27     hash_set_destroy(p->vars);
 28     hash_map_destroy(p->field_map);
 29 
 30     *(bool_t*)pv_output = true;
 31 }
 32 static void _alias_less(const void* cpv_first, const void* cpv_second, void* pv_output)
 33 {
 34     Alias* p1 = (Alias*)cpv_first;
 35     Alias* p2 = (Alias*)cpv_second;
 36 
 37     if (hash_set_less(p1->vars, p2->vars)) {
 38         *(bool_t*)pv_output = true;
 39         return;
 40     }
 41 
 42     if (hash_map_less(p1->field_map, p2->field_map)) {
 43         *(bool_t*)pv_output = true;
 44         return;
 45     }
 46 
 47     if (p1->status < p2->status) {
 48         *(bool_t*)pv_output = true;
 49         return;
 50     }
 51 
 52     *(bool_t*)pv_output = false;
 53 }
 54 static void _alias_copy(const void* cpv_first, const void* cpv_second, void* pv_output)
 55 {
 56     Alias* p1 = (Alias*)cpv_first;
 57     Alias* p2 = (Alias*)cpv_second;
 58 
 59     hash_set_assign(p1->vars, p2->vars);
 60     hash_map_assign(p1->field_map, p2->field_map);
 61     p1->status = p2->status;
 62 
 63     *(bool_t*)pv_output = true;
 64 }
 65 
 66 int main(int argc, char* argv[])
 67 {
 68     vector_t* pvec = NULL;
 69 
 70     type_register(Alias, _alias_init, _alias_copy, _alias_less, _alias_destroy);
 71     pvec = create_vector(Alias);
 72     if (pvec == NULL) {
 73         return -1;
 74     }
 75     vector_init_n(pvec, 10);
 76     printf("%d\n", vector_size(pvec));
 77 
 78     vector_destroy(pvec);
 79     return 0;
 80 }
 81                                                                
@activesys
Copy link
Owner Author

这个属于变成技巧问题,放到FAQ中。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant