list_head实践
''~``
( o o )
+------------------.oooO--(_)--Oooo.---------------------+
| Rick Wang |
| E-mail: rikyok@gmail.com |
| XIDIAN University http://www.xdwy.com.cn |
| Xi'an, China ( ) |
+---------------------\ (----( )-----------------------+
\_) ) /
(_/
( o o )
+------------------.oooO--(_)--Oooo.---------------------+
| Rick Wang |
| E-mail: rikyok@gmail.com |
| XIDIAN University http://www.xdwy.com.cn |
| Xi'an, China ( ) |
+---------------------\ (----( )-----------------------+
\_) ) /
(_/
1
2
#include <stdio.h>
3
#include "list.h"
4
5
struct test{
6
int a;
7
char b;
8
};
9
struct ts_hlist{
10
struct list_head member1;
11
int member2;
12
char member3;
13
double member4;
14
};
15
16
LIST_HEAD(hellolist);
17
struct ts_hlist hellots;
18
struct ts_hlist * rs;
19
int main(void){
20
21
struct test t1;
22
t1.a=8;
23
t1.b='z';
24
char *pchar="testok";
25
int i=0;
26
printf("length of int:%d char:%d double:%d long:%d\n",sizeof(int),sizeof(char),sizeof(double),sizeof(long));
27
printf("Address of t1 is:%p\nddd member of a is:%d\n member of b is:%c\n",&t1,t1.a,t1.b);
28
printf("Address of pchar is:%p\nvalue of pchar is:%s\n",pchar,pchar);
29
for(;i<strlen(pchar);i++)
30
{
31
printf("Address of member of pchar in pos %d is %p ,value of pchar in pos%d is:%c\n",i,pchar+i,i,pchar[i]);
32
}
33
printf("address base=0\n,than the address of a:%d\naddress of b:%d\n", &((struct test *)0)->a,&((struct test *)0)->b) ;
34
35
36
printf("If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:\n\n\n\n");
37
printf(" struct ts_hlist as NULL \n");
38
printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member1) );
39
printf(" |__________|\n");
40
printf(" |__________|\n");
41
printf(" |__________|\n");
42
printf(" |__________|\n");
43
printf(" |__________|\n");
44
printf(" |__________|\n");
45
printf(" |__________|\n");
46
printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member2) );
47
printf(" |__________|\n");
48
printf(" |__________|\n");
49
printf(" |__________|\n");
50
printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member3) );
51
printf(" |__________|\n");
52
printf(" |__________|\n");
53
printf(" |__________|\n");
54
printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member4) );
55
printf(" |__________|\n");
56
printf(" |__________|\n");
57
printf(" |__________|\n");
58
printf(" |__________|\n");
59
printf(" |__________|\n");
60
printf(" |__________|\n");
61
printf(" |__________|\n");
62
63
printf(" \n\n");
64
printf(" struct ts_hlist as allocate space\n");
65
int j=0;
66
67
printf(" size of ts_hlist struct is %d\n",sizeof(struct ts_hlist));
68
for(;j<sizeof(struct ts_hlist);j++)
69
{
70
printf(" |__________|<--%p\n",&hellots+(unsigned long)j);
71
}
72
printf(" the address of head(type of head_list) is:%p\n",hellolist);
73
74
hellots.member2=3;
75
hellots.member3='A';
76
hellots.member4=44.33;
77
list_add_tail(&hellots.member1,&hellolist);
78
rs=list_entry(&hellots.member1,struct ts_hlist,member1);
79
printf(" the address of member1 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member1) );
80
printf(" the address of member2 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member2) );
81
printf(" the address of member3 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member3) );
82
printf(" the address of member4 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member4) );
83
printf(" Now we'll test the value area of ts_hlist>>>>>\n");
84
printf(" member2 of rs is:%d\n", rs->member2);
85
printf(" member3 of rs is:%c\n", rs->member3);
86
printf(" member4 of rs is:%g\n", rs->member4);
87
}
88
89
90
Result:
91
92
93
If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:
94
95
96
struct ts_hlist as NULL
97
0 --->|__________|
98
|__________|
99
|__________|
100
|__________|
101
|__________|
102
|__________|
103
|__________|
104
|__________|
105
8 --->|__________|
106
|__________|
107
|__________|
108
|__________|
109
12--->|__________|
110
|__________|
111
|__________|
112
|__________|
113
16--->|__________|
114
|__________|
115
|__________|
116
|__________|
117
|__________|
118
|__________|
119
|__________|
120
|__________|
121
122
123
struct ts_hlist as allocate space
124
size of ts_hlist struct is 24
125
|__________|<--0x8049d7c
126
|__________|<--0x8049d94
127
|__________|<--0x8049dac
128
|__________|<--0x8049dc4
129
|__________|<--0x8049ddc
130
|__________|<--0x8049df4
131
|__________|<--0x8049e0c
132
|__________|<--0x8049e24
133
|__________|<--0x8049e3c
134
|__________|<--0x8049e54
135
|__________|<--0x8049e6c
136
|__________|<--0x8049e84
137
|__________|<--0x8049e9c
138
|__________|<--0x8049eb4
139
|__________|<--0x8049ecc
140
|__________|<--0x8049ee4
141
|__________|<--0x8049efc
142
|__________|<--0x8049f14
143
|__________|<--0x8049f2c
144
|__________|<--0x8049f44
145
|__________|<--0x8049f5c
146
|__________|<--0x8049f74
147
|__________|<--0x8049f8c
148
|__________|<--0x8049fa4
149
the address of head(type of head_list) is:0x8049c74
150
the address of member1 is: 0x8049d7c
151
the address of member2 is: 0x8049e3c
152
the address of member3 is: 0x8049e9c
153
the address of member4 is: 0x8049efc
154
Now we'll test the value aear of ts_hlist>>>>>
155
member2 of rs is:3
156
member3 of rs is:A
157
member4 of rs is:44.33

2
#include <stdio.h>3
#include "list.h"4

5
struct test{6
int a;7
char b;8
};9
struct ts_hlist{10
struct list_head member1;11
int member2;12
char member3;13
double member4;14
};15

16
LIST_HEAD(hellolist);17
struct ts_hlist hellots;18
struct ts_hlist * rs;19
int main(void){20

21
struct test t1;22
t1.a=8;23
t1.b='z';24
char *pchar="testok";25
int i=0;26
printf("length of int:%d char:%d double:%d long:%d\n",sizeof(int),sizeof(char),sizeof(double),sizeof(long));27
printf("Address of t1 is:%p\nddd member of a is:%d\n member of b is:%c\n",&t1,t1.a,t1.b);28
printf("Address of pchar is:%p\nvalue of pchar is:%s\n",pchar,pchar);29
for(;i<strlen(pchar);i++)30
{31
printf("Address of member of pchar in pos %d is %p ,value of pchar in pos%d is:%c\n",i,pchar+i,i,pchar[i]);32
}33
printf("address base=0\n,than the address of a:%d\naddress of b:%d\n", &((struct test *)0)->a,&((struct test *)0)->b) ;34

35

36
printf("If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:\n\n\n\n");37
printf(" struct ts_hlist as NULL \n");38
printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member1) );39
printf(" |__________|\n");40
printf(" |__________|\n");41
printf(" |__________|\n");42
printf(" |__________|\n");43
printf(" |__________|\n");44
printf(" |__________|\n");45
printf(" |__________|\n"); 46
printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member2) );47
printf(" |__________|\n");48
printf(" |__________|\n");49
printf(" |__________|\n");50
printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member3) );51
printf(" |__________|\n");52
printf(" |__________|\n");53
printf(" |__________|\n");54
printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member4) );55
printf(" |__________|\n");56
printf(" |__________|\n");57
printf(" |__________|\n");58
printf(" |__________|\n");59
printf(" |__________|\n");60
printf(" |__________|\n");61
printf(" |__________|\n");62

63
printf(" \n\n");64
printf(" struct ts_hlist as allocate space\n");65
int j=0;66

67
printf(" size of ts_hlist struct is %d\n",sizeof(struct ts_hlist));68
for(;j<sizeof(struct ts_hlist);j++)69
{70
printf(" |__________|<--%p\n",&hellots+(unsigned long)j);71
}72
printf(" the address of head(type of head_list) is:%p\n",hellolist);73

74
hellots.member2=3;75
hellots.member3='A';76
hellots.member4=44.33;77
list_add_tail(&hellots.member1,&hellolist);78
rs=list_entry(&hellots.member1,struct ts_hlist,member1);79
printf(" the address of member1 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member1) );80
printf(" the address of member2 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member2) );81
printf(" the address of member3 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member3) );82
printf(" the address of member4 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member4) );83
printf(" Now we'll test the value area of ts_hlist>>>>>\n");84
printf(" member2 of rs is:%d\n", rs->member2);85
printf(" member3 of rs is:%c\n", rs->member3);86
printf(" member4 of rs is:%g\n", rs->member4);87
}88

89

90
Result:91

92

93
If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:94

95

96
struct ts_hlist as NULL97
0 --->|__________|98
|__________|99
|__________|100
|__________|101
|__________|102
|__________|103
|__________|104
|__________|105
8 --->|__________|106
|__________|107
|__________|108
|__________|109
12--->|__________|110
|__________|111
|__________|112
|__________|113
16--->|__________|114
|__________|115
|__________|116
|__________|117
|__________|118
|__________|119
|__________|120
|__________|121

122

123
struct ts_hlist as allocate space124
size of ts_hlist struct is 24125
|__________|<--0x8049d7c126
|__________|<--0x8049d94127
|__________|<--0x8049dac128
|__________|<--0x8049dc4129
|__________|<--0x8049ddc130
|__________|<--0x8049df4131
|__________|<--0x8049e0c132
|__________|<--0x8049e24133
|__________|<--0x8049e3c134
|__________|<--0x8049e54135
|__________|<--0x8049e6c136
|__________|<--0x8049e84137
|__________|<--0x8049e9c138
|__________|<--0x8049eb4139
|__________|<--0x8049ecc140
|__________|<--0x8049ee4141
|__________|<--0x8049efc142
|__________|<--0x8049f14143
|__________|<--0x8049f2c144
|__________|<--0x8049f44145
|__________|<--0x8049f5c146
|__________|<--0x8049f74147
|__________|<--0x8049f8c148
|__________|<--0x8049fa4149
the address of head(type of head_list) is:0x8049c74150
the address of member1 is: 0x8049d7c151
the address of member2 is: 0x8049e3c152
the address of member3 is: 0x8049e9c153
the address of member4 is: 0x8049efc154
Now we'll test the value aear of ts_hlist>>>>>155
member2 of rs is:3156
member3 of rs is:A157
member4 of rs is:44.33



浙公网安备 33010602011771号