这三个月以来一直忙着赶进度,没有停下来记录一些东西,很多很好的东西往往只能零零散散地记在草稿本上, 这样偶尔想起来自己都找不到,所以现在抽空总结下来。
这些天做了三件事,其一是在Linux下开发了对接service的IPTV client lib on PC,之所以是on PC,因为我写的这段程序只是为了以后移植到一个运行RTOS的机顶盒上面;其二是基于PayPal做的支付系统,其三是一个监控服务器和用户状态的简单后台管理页面,这两个都是用cakephp + bootstrap做的,并没有涉及到数据库,与数据库交互是使用了已经写好的API。这篇记录C的内容。
一、打印指针值:
int a = 0; int *p = &a; printf("%p", p);
二、
PHPStorm: PHP IDE
PyCharm: PYTHON IDE
Brackets : 强大免费的开源跨平台Web前端开发工具IDE
coolshell.com : 酷壳网
三、
问题:VMWare Workstation虚拟机出现故障,非正常关机时,无法再次打开。
解决:删除虚拟磁盘目录下的*.lck文件(可能需要重启)。
四、字符串的正确使用:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char str1[27] = "abcdefghijklmnopqrstuvwxyz";//编译器将在末尾自动添加“”,但是需要保证,字符数组中有空余的位置 char *str2 = NULL; str2 = (char *)malloc(26 + 1); memset(str2, 0, 26 + 1); memcpy(str2, str1, 26); printf("str1 length is %ld, size is %ld ", strlen(str1), sizeof(str1)); printf("str2 length is %ld, size is %ld ", strlen(str2), sizeof(str2)); int i = 0; for(i = 0; i < 30; i++) printf("%02x ", str1[i]); printf(" "); for(i = 0; i < 30; i++) printf(" %c ", str1[i]); printf(" "); if(NULL != str2) free(str2); return 0; }
输出:
hubery@hubery-VirtualBox:~/CP/HW$ ./shellInput str1 length is 26, size is 27 str2 length is 26, size is 8 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 00 00 00 00 a b c d e f g h i j k l m n o p q r s t u v w x y z
尽量不要使用strlen、strcpy、strcat这些需要通过结束符“”来判断字符串长度的函数,因为一旦字符串没有正常的结束符,你将得到意想不到的结果。
五、在子函数中malloc变量(声明时字符串大小不可知):
#include <stdlib.h> #include <stdio.h> int malloc_inner(char **p) { *p = (char *)malloc(10); return 0; } int main() { char *str = null; malloc_inner(&str); free(str); return 0; }
六、命令行参数:
//shellInput.c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { printf("argc = %d, argv[0] = %s, argv[1] = %s ", argc, argv[0], argv[1]); return 0; } //output #./shellInput hello argc = 2, argv[0] = ./shellInput, argv[1] = hello
七、使用CGDB调试一个程序:
objdump,cgdb,dmesg -c.
编译参数: -g -rdynamic
调试: cgdb ./a.out
加断点: b func_name
步进: n
步入: s
打印: p
GDB/CGDB 调试时打印完整内容:set print element 0
查看dumped的位置: bt/where
使down一个程序: kill -s SIGSEGV pid
八、C读写文件:
snprintf(filename, sizeof(filename), "%s.sig", arv[2]); //write if((f = fopen(filename, "wb+")) == NULL) { ret = 1; printf("..."); //... } if(fwrite(buf, 1, olen ,f) != olen) { printf("..."); //... } fclose(f); //read f = fopen(filename,"rb"); i = fread(buf, 1, sizeof(buf), f); fclose(f);
九、结构体
typedef struct { slist_node node; VdsChannel channel; CHAR *type; CHAR *video_id; CHAR *url;//需要url时,先查询链表,若没有再作请求 }VdsChanInfo; struct timerr { time_t time_begin; unsigned int time_long; unsigned int time_refresh; char user_id[50]; char token[32]; }; struct timerr timer ;//用法1 //结构体数组 struct { UINT32 isValid;//1 for true, 0 for false UINT32 decode_type;//0 for all, 1 for sha1+rsa, 2 for aes+rsa CHAR *request_encrypt_key; CHAR *response_encrypt_key; }keyPairs[KEY_PAIR_SUM]; //构造枚举类型 typedef enum { VDS_READY, VDS_AUTH_OK, VDS_GET_KEY_OK, VDS_GET_CHAN_INIT_OK, VDS_GET_CHAN_URL_OK }VdsStatus; typedef struct { BOOL running; BOOL exit; VdsStatus status; VdsIptvParam param; pthread_t thread_id; pthread_mutex_t lock; pthread_mutex_t debug_lock; UINT32 chan_num; slist channel_list; }VdsIptvMgr; VdsIptvMgr * get_mgr(void);//用法2
十、链表使用

1 //singly_linked_list.h 2 #ifndef __SINGLY_LINKED_LIST_H__ 3 #define __SINGLY_LINKED_LIST_H__ 4 #include "porting.h" 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 typedef struct _slist_node { 11 struct _slist_node *next; 12 } slist_node; 13 14 typedef struct { 15 UINT32 count; 16 slist_node *head; 17 slist_node *tail; 18 } slist; 19 20 static __inline__ void slist_add_head(slist *list, slist_node *node) { 21 if (list->count == 0) { 22 list->head = node; 23 list->tail = node; 24 } 25 else { 26 node->next = list->head; 27 list->head = node; 28 } 29 list->count++; 30 } 31 32 static __inline__ void slist_add_tail(slist *list, slist_node *node) 33 { 34 if (list->count == 0) { 35 list->head = node; 36 list->tail = node; 37 } 38 else { 39 list->tail->next = node; 40 list->tail = node; 41 } 42 list->count++; 43 } 44 45 static __inline__ slist_node * slist_del_head(slist *list) 46 { 47 slist_node *node = NULL; 48 if (list->count > 0) { 49 node = list->head; 50 list->head = node->next; 51 node->next = NULL; 52 if (--list->count == 0) { 53 list->tail = NULL; 54 } 55 } 56 return node; 57 } 58 59 static __inline__ slist_node * slist_del_tail(slist *list) { 60 slist_node *node = NULL; 61 if (list->count > 1) { 62 slist_node *ptr = list->head; 63 while (ptr) { 64 if (ptr->next == list->tail) { 65 node = list->tail; 66 ptr->next = NULL; 67 list->tail = ptr; 68 list->count--; 69 break; 70 } 71 ptr = ptr->next; 72 } 73 } 74 else if (list->count == 1) { 75 node = list->tail; 76 list->head = list->tail = NULL; 77 list->count--; 78 } 79 80 return node; 81 } 82 83 static __inline__ slist_node * slist_del_node(slist *list, slist_node *node) 84 { 85 slist_node *curr = NULL; 86 slist_node *temp = NULL; 87 88 if(list->head == node) 89 return slist_del_head(list); 90 91 if(list->tail == node) 92 return slist_del_tail(list); 93 94 //if(list->count <= 2) 95 //{ 96 //ASSERT(0); 97 //} 98 99 curr = list->head; 100 if(curr == NULL) 101 return NULL; 102 103 while(curr->next != NULL) 104 { 105 if(curr->next == node) 106 { 107 temp = curr->next; 108 curr->next = temp->next; 109 list->count--; 110 111 112 temp->next = NULL; 113 114 /* 115 if(list->count >= 1) 116 { 117 if(list->head == NULL || list->tail == NULL) 118 { 119 SDBBP(); 120 } 121 122 if(list->count == 1 && list->head != list->tail) 123 { 124 SDBBP(); 125 } 126 127 if(list->count == 2 && list->head->next != list->tail) 128 { 129 SDBBP(); 130 } 131 132 if(list->tail->next != NULL) 133 { 134 SDBBP(); 135 } 136 } 137 */ 138 139 return temp; 140 } 141 else 142 curr = curr->next; 143 } 144 145 /* 146 if(list->count > 0) 147 { 148 if(list->tail->next != NULL) 149 { 150 SDBBP(); 151 } 152 } 153 */ 154 return NULL; 155 } 156 157 static __inline__ void slist_free(slist *list) 158 { 159 slist_node *node = NULL; 160 if(NULL == list) 161 return; 162 while(list->count > 0) 163 { 164 node = slist_del_head(list); 165 FREE(node); 166 } 167 } 168 169 #define SLIST_COUNT(slist) ((slist)->count) 170 171 /** 172 * SLIST_ENTRY - get the struct for this entry 173 * @ptr: the struct slist_node pointer. 174 * @type: the type of the struct @ptr embedded in. 175 * @member: the name of the slist_node within the struct. 176 */ 177 #define SLIST_ENTRY(ptr, type, member) 178 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) 179 180 181 #ifdef __cplusplus 182 } 183 #endif /* __cplusplus */ 184 185 #endif
下面展示如何使用以上链表操作。
// typedef struct { UINT32 idx; CHAR *name; CHAR *img_h; CHAR *img_s; }VdsChannel; typedef struct { slist_node node; VdsChannel channel; CHAR *type; CHAR *video_id; CHAR *url;//需要url时,先查询链表,若没有再作请求 }VdsChanInfo; //declare vds_list = (slist *)malloc(sizeof(slist)); vds_list->count = 0; vds_list->head = NULL; vds_list->tail = NULL; //add VdsChanInfo *channels = (VdsChanInfo *)malloc(sizeof(VdsChanInfo)); channels->type = ( CHAR * )malloc( strlen( value ) + 1 ); channels->video_id = ( CHAR * )malloc( strlen( value ) + 1 ); channels->channel.name = ( CHAR * )malloc( strlen( value ) + 1 ); channels->channel.img_h = ( CHAR * )malloc( strlen( value ) + 1 ); channels->channel.img_s = ( CHAR * )malloc( strlen( value ) + 1 ); strcpy( channels->type , value ); strcpy( channels->video_id, value ); strcpy( channels->channel.name, value ); strcpy( channels->channel.img_h, value ); strcpy( channels->channel.img_s, value ); channels->channel.idx = i; channels->url = NULL; channels->node.next = NULL; slist_add_tail(list, &(channels->node)); //search slist_node *node; VdsChanInfo *vds; VdsChannel *channel_info = (VdsChannel *)malloc(sizeof(VdsChannel));; for(node = vds_list->head; node != NULL; node = node->next) { vds = SLIST_ENTRY(node, VdsChanInfo, node); if(idx == vds->channel.idx) { channel_info->idx = vds->channel.idx; channel_info->img_h = vds->channel.img_h; channel_info->img_s = vds->channel.img_s; channel_info->name = vds->channel.name; VDS_DBG(VDS_D_SYS, " . Got channel info."); return channel_info; } } //free and destory void vds_free(slist *list) { slist_node *node; VdsChanInfo *vds; for(node = list->head; node != NULL; node = node->next) { vds = SLIST_ENTRY(node, VdsChanInfo, node); if(NULL != vds->type) { free(vds->type); vds->type = NULL; } if(NULL != vds->video_id) { free(vds->video_id); vds->video_id = NULL; } if(NULL != vds->url) { free(vds->url); vds->url = NULL; } if(NULL != vds->channel.name) { free(vds->channel.name); vds->channel.name = NULL; } if(NULL != vds->channel.img_h) { free(vds->channel.img_h); vds->channel.img_h = NULL; } if(NULL != vds->channel.img_s) { free(vds->channel.img_s); vds->channel.img_s = NULL; } } // slist_free(vds_list); } vds_free(vds_list); slist_free(vds_list);