一開始運行出錯,開啓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的內部實現了,不想再深刻去了解,暫時到此爲止。
最後仍是照慣例貼個代碼:less
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 }