结构体最后的长度为0或者1的数组
2012-02-08 17:36
465 查看
文章出处:http://www.cnblogs.com/sweetdream/archive/2007/06/10/778308.html
在Linux系统里,/usr/include/linux/if_pppox.h里面有这样一个结构:
......
释放时,
free(sample_tag)
是否可以用char *tag_data代替呢?其实它和char *tag_data是有很大的区别,为了说明这个问题,我写了以下的程序:
例:test_size.c
__attribute ((packed))是为了强制不进行字节对齐,这样比较容易说明问题。
程序的运行结果如下:
size of tag1 = 8
size of tag2 = 12
size of tag3 = 8
size of tag4 = 9
l_tag2 = 0xbffffad0,&l_tag2.c = 0xbffffad8,l_tag2.c = (nil)
l_tag3 = 0xbffffac8,l_tag3.c = 0xbffffad0
l_tag4 = 0xbffffabc,l_tag4.c = 0xbffffac4
从上面程序的运行结果可以看出:tag1本身包括两个位整数,所以占用了8个字节的空间。tag2包括了两个位的整数,外加一个char *的指针,所以占用了12个字节。tag3才是真正看出char c[0]和char *c的区别,char
c[0]中的c并不是指针,是一个偏移量,这个偏移量指向的是a、b后面紧接着的空间,所以它其实不占用任何空间。tag4更加补充说明了这一点。所以,上面的struct pppoe_tag的最后一个成员如果用char *tag_data定义,除了会占用多个字节的指针变量外,用起来会比较不方便:
方法一:创建时,可以首先为struct pppoe_tag分配一块内存,在为tag_data分配内存,这样在释放时,要首先释放tag_data占用的内存,再释放pppoe_tag占用的内存;
方法二,创建时,直接为struct pppoe_tag分配一块struct pppoe_tag大小加上tag_data的内存,从例一的行可以看出,tag_data的内容要进行初始化,要让tag_data指向struct pppoe_tag后面的内存。
讲了这么多,其实本质上涉及到的是一个C语言里面的数组和指针的区别问题。char
a[1]里面的a 和 char *b的 b 相同吗? 《Programming Abstractions in C》(Roberts,E.S., 机械工业出版社,.6)页里面说:"arr is defined to be indetical to &arr[0]"。也就是说,char a[1]里面的a实际上是一个常量,等于&a[0](数组名代表数组的首地址)。而char
*b是有一个实实在在的指针变量b存在。所以,a=b是不允许的,而b=a是允许的。两种变量都支持下标式的访问,那么对于a[0]和b[0]本质上是否有区别?我们可以通过一个例子来说明。
例二:
可以看出,a[2]=xfe是直接寻址,直接将xfe写入&a[0]+2的地址,而b[2]=0xfe是间接寻址,先将b的内容(地址)拿出来,加,再xfe写入计算出来的地址。
所以a[0]和b[0]本质上是不同的。但当数组作为参数时,和指针就没有区别了。
顺便再说一下,对于struct pppoe_tag的最后一个成员的定义是char tag_data[0],某些编译器不支持长度为0的数组的定义,在这种情况下,只能将它定义成char
tag_data[1],使用方法相同。
在Linux系统里,/usr/include/linux/if_pppox.h里面有这样一个结构:
struct pppoe_tag { __u16 tag_type; __u16 tag_len; char tag_data[0]; } __attribute ((packed));最后一个成员为可变长的数组,对于TLV(Type-Length-Value)形式的结构,或者其他需要变长度的结构体,用这种方式定义最好。使用起来非常方便,创建时,malloc一段结构体大小加上可变长数据长度的空间给它,可变长部分可按数组的方式访问,释放时,直接把整个结构体free掉就可以了。例子如下:
struct pppoe_tag *sample_tag; __u16 sample_tag_len = 10; sample_tag = (struct pppoe_tag *)malloc(sizeof(struct pppoe_tag)+sizeof(char)*sample_tag_len); sample_tag->tag_type = 0xffff; sample_tag->tag_len = sample_tag_len; sample_tag->tag_data[0]=....
......
释放时,
free(sample_tag)
是否可以用char *tag_data代替呢?其实它和char *tag_data是有很大的区别,为了说明这个问题,我写了以下的程序:
例:test_size.c
10 struct tag1 20 { 30 int a; 40 int b; 50 }__attribute ((packed)); 60 70 struct tag2 80 { 90 int a; 100 int b; 110 char *c; 120 }__attribute ((packed)); 130 140 struct tag3 150 { 160 int a; 170 int b; 180 char c[0]; 190 }__attribute ((packed)); 200 210 struct tag4 220 { 230 int a; 240 int b; 250 char c[1]; 260 }__attribute ((packed)); 270 280 int main() 290 { 300 struct tag2 l_tag2; 310 struct tag3 l_tag3; 320 struct tag4 l_tag4; 330 340 memset(&l_tag2,0,sizeof(struct tag2)); 350 memset(&l_tag3,0,sizeof(struct tag3)); 360 memset(&l_tag4,0,sizeof(struct tag4)); 370 380 printf("size of tag1 = %d\n",sizeof(struct tag1)); 390 printf("size of tag2 = %d\n",sizeof(struct tag2)); 400 printf("size of tag3 = %d\n",sizeof(struct tag3)); 410 420 printf("l_tag2 = %p,&l_tag2.c = %p,l_tag2.c = %p\n",&l_tag2,&l_tag2.c,l_tag2.c); 430 printf("l_tag3 = %p,l_tag3.c = %p\n",&l_tag3,l_tag3.c); 440 printf("l_tag4 = %p,l_tag4.c = %p\n",&l_tag4,l_tag4.c); 450 exit(0); 460 }
__attribute ((packed))是为了强制不进行字节对齐,这样比较容易说明问题。
程序的运行结果如下:
size of tag1 = 8
size of tag2 = 12
size of tag3 = 8
size of tag4 = 9
l_tag2 = 0xbffffad0,&l_tag2.c = 0xbffffad8,l_tag2.c = (nil)
l_tag3 = 0xbffffac8,l_tag3.c = 0xbffffad0
l_tag4 = 0xbffffabc,l_tag4.c = 0xbffffac4
从上面程序的运行结果可以看出:tag1本身包括两个位整数,所以占用了8个字节的空间。tag2包括了两个位的整数,外加一个char *的指针,所以占用了12个字节。tag3才是真正看出char c[0]和char *c的区别,char
c[0]中的c并不是指针,是一个偏移量,这个偏移量指向的是a、b后面紧接着的空间,所以它其实不占用任何空间。tag4更加补充说明了这一点。所以,上面的struct pppoe_tag的最后一个成员如果用char *tag_data定义,除了会占用多个字节的指针变量外,用起来会比较不方便:
方法一:创建时,可以首先为struct pppoe_tag分配一块内存,在为tag_data分配内存,这样在释放时,要首先释放tag_data占用的内存,再释放pppoe_tag占用的内存;
方法二,创建时,直接为struct pppoe_tag分配一块struct pppoe_tag大小加上tag_data的内存,从例一的行可以看出,tag_data的内容要进行初始化,要让tag_data指向struct pppoe_tag后面的内存。
struct pppoe_tag { __u16 tag_type; __u16 tag_len; char *tag_data; } __attribute ((packed)); struct pppoe_tag *sample_tag; __u16 sample_tag_len = 10; 方法一: sample_tag = (struct pppoe_tag *)malloc(sizeof(struct pppoe_tag)); sample_tag->tag_len = sample_tag_len; sample_tag->tag_data = malloc(sizeof(char)*sample_tag_len); sample_tag->tag_data[0]=... 释放时: free(sample_tag->tag_data); free(sample_tag); 方法二: sample_tag = (struct pppoe_tag *)malloc(sizeof(struct pppoe_tag)+sizeof(char)*sample_tag_len); sample_tag->tag_len = sample_tag_len; sample_tag->tag_data = ((char *)sample_tag)+sizeof(struct pppoe_tag); sample_tag->tag_data[0]=... 释放时: free(sample_tag);所以无论使用那种方法,都没有char tag_data[0]这样的定义来的方便。
讲了这么多,其实本质上涉及到的是一个C语言里面的数组和指针的区别问题。char
a[1]里面的a 和 char *b的 b 相同吗? 《Programming Abstractions in C》(Roberts,E.S., 机械工业出版社,.6)页里面说:"arr is defined to be indetical to &arr[0]"。也就是说,char a[1]里面的a实际上是一个常量,等于&a[0](数组名代表数组的首地址)。而char
*b是有一个实实在在的指针变量b存在。所以,a=b是不允许的,而b=a是允许的。两种变量都支持下标式的访问,那么对于a[0]和b[0]本质上是否有区别?我们可以通过一个例子来说明。
例二:
10 #include <stdio.h> 20 #include <stdlib.h> 30 40 int main() 50 { 60 char a[10]; 70 char *b; 80 90 a[2]=0xfe; 100 b[2]=0xfe; 110 exit(0); 120 }编译后,用objdump可以看到它的汇编:
080483f0 <main>: 80483f0: 55 push %ebp 80483f1: 89 e5 mov %esp,%ebp 80483f3: 83 ec 18 sub $0x18,%esp 80483f6: c6 45 f6 fe movb $0xfe,0xfffffff6(%ebp) 80483fa: 8b 45 f0 mov 0xfffffff0(%ebp),%eax 80483fd: 83 c0 02 add $0x2,%eax 8048400: c6 00 fe movb $0xfe,(%eax) 8048403: 83 c4 f4 add $0xfffffff4,%esp 8048406: 6a 00 push $0x0 8048408: e8 f3 fe ff ff call 8048300 <_init+0x68> 804840d: 83 c4 10 add $0x10,%esp 8048410: c9 leave 8048411: c3 ret 8048412: 8d b4 26 00 00 00 00 lea 0x0(%esi,1),%esi 8048419: 8d bc 27 00 00 00 00 lea 0x0(%edi,1),%edi
可以看出,a[2]=xfe是直接寻址,直接将xfe写入&a[0]+2的地址,而b[2]=0xfe是间接寻址,先将b的内容(地址)拿出来,加,再xfe写入计算出来的地址。
所以a[0]和b[0]本质上是不同的。但当数组作为参数时,和指针就没有区别了。
int do1(char a[],int len); int do2(char *a,int len);这两个函数中的a并无任何区别,都是实实在在的指针变量。
顺便再说一下,对于struct pppoe_tag的最后一个成员的定义是char tag_data[0],某些编译器不支持长度为0的数组的定义,在这种情况下,只能将它定义成char
tag_data[1],使用方法相同。
相关文章推荐
- 结构体最后的长度为0或者1的数组
- GLib中私有数据的表示 和 结构体最后的长度为0或者1的数组
- 结构体最后的长度为0或者1的数组[转]
- [转]结构体最后的长度为0或者1的数组
- 结构体最后的长度为0或者1的数组[转]
- 结构体最后的长度为0或者1的数组
- 结构体最后的长度为0或者1的数组
- 结构体最后的长度为0或者1的数组
- 在结构体最后定义一个长度为0的字符数组(技巧)
- 结构体最后的长度为0或1数组的作用(转载)
- 结构体最后的长度为0或1数组的作用(转载)
- 结构体最后的长度为0或1数组的作用
- 【C语言】关于结构体最后的长度为0或1数组的思考
- 结构体最后的长度为0或1数组的作用
- 结构体最后的长度为0或者1的数组
- 结构体最后的长度为0或者1的数组
- 在结构体最后定义一个长度为0的字符数组(技巧)
- 结构体最后的长度为0或1数组的作用
- 结构体最后的长度为0或1数组的作用
- 结构体中最后成员为一个数组(长度为零)与一个指针