c/c++常用技巧(更新中)

1. double -> int   //directx9c以上失效,因为其会改变单浮点精度

ftol



2. c双向链表操作

list

container_of 宏
1/*  GCC   */
2#define container_of(ptr, type, member) ({            \   
3        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \   
4        (type *)( (char *)__mptr - offsetof(type,member) );})   
5
6//or
7
8#define container_of(ptr, type, member) (            \   
9        (type *)( (char *)ptr - offsetof(type,member) ) )  

how to use
 1struct my_data {   
 2    int x;   
 3    int y;   
 4    struct list_head list;   
 5}
   
 6  
 7/* 链表头 */   
 8LIST_HEAD(my_listhead);   
 9  
10void my_function()   
11{   
12       
13    /* 节点对象 */   
14    struct my_data *node_1 = (struct my_data *) malloc(sizeof(struct my_data));   
15    struct my_data *node_2 = (struct my_data *) malloc(sizeof(struct my_data));   
16       
17    /* 加入链表 */   
18    list_add (node_1->list, &my_listhead);   
19    list_add (node_2->list, &my_listhead);   
20       
21    /* 遍历链表 */   
22    struct my_data * node;   
23    struct list_head *pos;   
24    list_for_each (pos, &my_listhead) {   
25       node = list_entry (pos, struct my_data, list);   
26          
27    }
   

posted on 2008-06-13 11:15  yyliuliang  阅读(363)  评论(0编辑  收藏  举报

导航