libcstl中的list没法插入自定义数据
一开始运行出错,开启debug以后发现在push自定义对象的时候调试器提示找不到一个叫/XXX/XXXX/XXXX/libcstl-2.3.0/src/cstl_list_private.c</br>
而那个路径正是我进行安装的路径,安装完以后我把安装包给删除掉了,所以它找不到。这样的话,我们在一开始安装的时候就要注意最好先把tar.gz解压出来的文件夹放到特定文件夹,比如/usr/local/下,这样不会在安装完成后被误删,也比较方便查找。</br>
但是再次调试的时候却发现插入操作死在了调用_my_copy()函数那里,里面设置了一个int型临时变量i_temp用来作中间值方便调试,调试过程中发现i_temp成了一个非常小的负值。
但是直接调用_my_copy()是能正确运行的。</br>
鉴于程序呈现出这种尿性,我觉得应该是cstl它自己设计得不够健壮。。。不然实在是说不通程序会死在_my_copy()函数那里。
其实最后我发现copy函数形参里的cpv_source它的地址为0x1,明显就不可能。这个就关系到cstl对list的内部实现了,不想再深入去了解,暂时到此为止。
最后还是照惯例贴个代码:
1 /* 2 * new_test_for_ctsl_selfType.c 3 * 4 * Created on: Mar 21, 2014 5 * Author: nerohwang 6 */ 7 #include<stdio.h> 8 #include<stdlib.h> 9 #include<cstl/clist.h> 10 #include<assert.h> 11 /*Initlizing a user-defined type ,use 12 * func type_register(1,2,3,4,5) first. 13 * 1.type: user-defined type 14 * 2.ufun_init: init function 15 * 3.bfun_copy: copy function 16 * 4.bfun_less: less-comparison function 17 * 5.ufun_destroy: destroy function 18 19 bool_t type_register( 20 type, 21 unary_function_t ufun_init, 22 binary_function_t bfun_copy, 23 binary_function_t bfun_less, 24 unary_function_t ufun_destroy 25 ); 26 * 27 */ 28 typedef struct user_defined_type 29 { 30 int i_first; 31 int i_second; 32 }myType; 33 34 static void _my_init(const void* cpv_input,void* pv_output) 35 { 36 assert(cpv_input != NULL); 37 ((myType*)cpv_input)->i_first = 8; 38 ((myType*)cpv_input)->i_second = 9; 39 *((bool_t*)pv_output) = true; 40 } 41 42 static void _my_copy(const void* cpv_dest,const void* cpv_source,void* pv_output) 43 { 44 assert(cpv_dest != NULL && cpv_source != NULL); 45 int i_temp = ((myType*)cpv_source)->i_first; 46 ((myType*)cpv_dest)->i_first = i_temp; 47 i_temp = ((myType*)cpv_source)->i_second; 48 ((myType*)cpv_dest)->i_second = i_temp; 49 *((bool_t*)pv_output) = true; 50 } 51 52 static void _my_destroy(const void* cpv_input,void* pv_output) 53 { 54 assert(cpv_input != NULL); 55 ((myType*)cpv_input)->i_first = 0; 56 ((myType*)cpv_input)->i_second = 0; 57 *((bool_t*)pv_output) = true; 58 } 59 60 static void _my_less(const void* cpv_first, const void* cpv_second,void* pv_output) 61 { 62 assert(cpv_first != NULL && cpv_second != NULL); 63 *((bool_t*)pv_output) = (((myType*)cpv_first)->i_first < ((myType*)cpv_second)->i_first)?true:false; 64 } 65 66 int main(int argc,char* argv[]) 67 { 68 list_t* pList = create_list(myType); 69 list_iterator_t i_it; 70 list_iterator_t my_it; 71 printf("Before type register:\n"); 72 if(pList == NULL){ 73 printf("Creation of myType failed!\n"); 74 }else{ 75 printf("Creation of myType succeeded!\n"); 76 } 77 type_register(myType,_my_init,_my_copy,_my_less,_my_destroy); 78 79 pList = create_list(myType); 80 printf("After type register:\n"); 81 if(pList != NULL){ 82 printf("Creation of myType succeeded!\n"); 83 }else{ 84 printf("Creation of myType failed!\n"); 85 } 86 87 //just a simple test. 88 myType my_first; 89 my_first.i_first = 1; 90 my_first.i_second = 2; 91 printf("first :one-> %d,sec-> %d\n",my_first.i_first,my_first.i_second); 92 93 myType my_second; //default 94 95 myType my_third; 96 my_third.i_first = 12; 97 my_third.i_second = 13; 98 99 list_t* pList_i = create_list(int); 100 if(pList_i == NULL){ 101 printf("Creation of int list failed!\n"); 102 } 103 list_init(pList_i); 104 list_push_back(pList_i,3); 105 list_push_back(pList_i,8); 106 list_push_back(pList_i,7); 107 printf("Now we have %d int-var in our list\n",list_size(pList_i)); 108 for(i_it = list_begin(pList_i);!iterator_equal(i_it,list_end(pList_i));i_it = iterator_next(i_it)) 109 { 110 printf("%d\t",*(int*)iterator_get_pointer(i_it)); 111 } 112 printf("\n"); 113 114 bool_t b_temp; 115 _my_copy((void*)&my_second,(void*)&my_first,(void*)&b_temp); 116 printf("Second :one-> %d,sec-> %d\n",my_second.i_first,my_second.i_second); 117 118 printf("break point\n"); 119 list_init(pList); 120 list_push_back(pList,my_second); 121 my_it = list_begin(pList); 122 printf("Second myType: one-> %d , sec->%d\n",((myType*)iterator_get_pointer(my_it))->i_first,\ 123 ((myType*)iterator_get_pointer(my_it))->i_second); 124 125 126 printf("break point\n"); 127 list_push_back(pList,my_first); 128 list_push_back(pList,my_third); 129 printf("Now we have %d obj in our list\n",list_size(pList)); 130 return 0; 131 132 }
/***Hello World***/