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
5struct test{
6int a;
7char b;
8};
9struct ts_hlist{
10struct list_head member1;
11int member2;
12char member3;
13double member4;
14};
15
16LIST_HEAD(hellolist);
17struct ts_hlist hellots;
18struct ts_hlist * rs;
19int main(void){
20
21struct test t1;
22t1.a=8;
23t1.b='z';
24char *pchar="testok";
25int i=0;
26printf("length of int:%d char:%d double:%d long:%d\n",sizeof(int),sizeof(char),sizeof(double),sizeof(long));
27printf("Address of t1 is:%p\nddd member of a is:%d\n member of b is:%c\n",&t1,t1.a,t1.b);
28printf("Address of pchar is:%p\nvalue of pchar is:%s\n",pchar,pchar);
29for(;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}
33printf("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
36printf("If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:\n\n\n\n");
37printf(" struct ts_hlist as NULL \n");
38printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member1) );
39printf(" |__________|\n");
40printf(" |__________|\n");
41printf(" |__________|\n");
42printf(" |__________|\n");
43printf(" |__________|\n");
44printf(" |__________|\n");
45printf(" |__________|\n");
46printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member2) );
47printf(" |__________|\n");
48printf(" |__________|\n");
49printf(" |__________|\n");
50printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member3) );
51printf(" |__________|\n");
52printf(" |__________|\n");
53printf(" |__________|\n");
54printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member4) );
55printf(" |__________|\n");
56printf(" |__________|\n");
57printf(" |__________|\n");
58printf(" |__________|\n");
59printf(" |__________|\n");
60printf(" |__________|\n");
61printf(" |__________|\n");
62
63printf(" \n\n");
64printf(" struct ts_hlist as allocate space\n");
65int j=0;
66
67printf(" size of ts_hlist struct is %d\n",sizeof(struct ts_hlist));
68for(;j<sizeof(struct ts_hlist);j++)
69{
70 printf(" |__________|<--%p\n",&hellots+(unsigned long)j);
71}
72printf(" the address of head(type of head_list) is:%p\n",hellolist);
73
74hellots.member2=3;
75hellots.member3='A';
76hellots.member4=44.33;
77list_add_tail(&hellots.member1,&hellolist);
78rs=list_entry(&hellots.member1,struct ts_hlist,member1);
79printf(" the address of member1 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member1) );
80printf(" the address of member2 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member2) );
81printf(" the address of member3 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member3) );
82printf(" the address of member4 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member4) );
83printf(" Now we'll test the value area of ts_hlist>>>>>\n");
84printf(" member2 of rs is:%d\n", rs->member2);
85printf(" member3 of rs is:%c\n", rs->member3);
86printf(" member4 of rs is:%g\n", rs->member4);
87}
88
89
90Result:
91
92
93If 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
970 --->|__________|
98 |__________|
99 |__________|
100 |__________|
101 |__________|
102 |__________|
103 |__________|
104 |__________|
1058 --->|__________|
106 |__________|
107 |__________|
108 |__________|
10912--->|__________|
110 |__________|
111 |__________|
112 |__________|
11316--->|__________|
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
5struct test{
6int a;
7char b;
8};
9struct ts_hlist{
10struct list_head member1;
11int member2;
12char member3;
13double member4;
14};
15
16LIST_HEAD(hellolist);
17struct ts_hlist hellots;
18struct ts_hlist * rs;
19int main(void){
20
21struct test t1;
22t1.a=8;
23t1.b='z';
24char *pchar="testok";
25int i=0;
26printf("length of int:%d char:%d double:%d long:%d\n",sizeof(int),sizeof(char),sizeof(double),sizeof(long));
27printf("Address of t1 is:%p\nddd member of a is:%d\n member of b is:%c\n",&t1,t1.a,t1.b);
28printf("Address of pchar is:%p\nvalue of pchar is:%s\n",pchar,pchar);
29for(;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}
33printf("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
36printf("If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:\n\n\n\n");
37printf(" struct ts_hlist as NULL \n");
38printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member1) );
39printf(" |__________|\n");
40printf(" |__________|\n");
41printf(" |__________|\n");
42printf(" |__________|\n");
43printf(" |__________|\n");
44printf(" |__________|\n");
45printf(" |__________|\n");
46printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member2) );
47printf(" |__________|\n");
48printf(" |__________|\n");
49printf(" |__________|\n");
50printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member3) );
51printf(" |__________|\n");
52printf(" |__________|\n");
53printf(" |__________|\n");
54printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member4) );
55printf(" |__________|\n");
56printf(" |__________|\n");
57printf(" |__________|\n");
58printf(" |__________|\n");
59printf(" |__________|\n");
60printf(" |__________|\n");
61printf(" |__________|\n");
62
63printf(" \n\n");
64printf(" struct ts_hlist as allocate space\n");
65int j=0;
66
67printf(" size of ts_hlist struct is %d\n",sizeof(struct ts_hlist));
68for(;j<sizeof(struct ts_hlist);j++)
69{
70 printf(" |__________|<--%p\n",&hellots+(unsigned long)j);
71}
72printf(" the address of head(type of head_list) is:%p\n",hellolist);
73
74hellots.member2=3;
75hellots.member3='A';
76hellots.member4=44.33;
77list_add_tail(&hellots.member1,&hellolist);
78rs=list_entry(&hellots.member1,struct ts_hlist,member1);
79printf(" the address of member1 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member1) );
80printf(" the address of member2 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member2) );
81printf(" the address of member3 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member3) );
82printf(" the address of member4 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member4) );
83printf(" Now we'll test the value area of ts_hlist>>>>>\n");
84printf(" member2 of rs is:%d\n", rs->member2);
85printf(" member3 of rs is:%c\n", rs->member3);
86printf(" member4 of rs is:%g\n", rs->member4);
87}
88
89
90Result:
91
92
93If 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
970 --->|__________|
98 |__________|
99 |__________|
100 |__________|
101 |__________|
102 |__________|
103 |__________|
104 |__________|
1058 --->|__________|
106 |__________|
107 |__________|
108 |__________|
10912--->|__________|
110 |__________|
111 |__________|
112 |__________|
11316--->|__________|
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