�������


  ��前位�茫� > ���蚩� > ������� > �酆����� 
 
�构�����某���为0����1������  
 
����:googol4u 时��:2003-01-19 11:03 ����:������ ���:chinaitpower  
 
              摘要���构�����某���为0����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 ���泻艽�����为
��说��������,��写�����碌某���
��1��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)) ��为��强�撇�����4�纸诙��,����冉�����说�����。
��������薪�����拢�
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�����������32位��������占��8���纸诘目占�
��tag2����������32位���������一��char *��指�,����占��12���纸凇�tag3����
�����char c[0]��char *c�����char c[0]�械�c������指�,��一��偏��������
��偏����指�����a��b��������的空间,��������实����占���何空间。tag4��硬�
��说������一�。���裕������struct pppoe_tag�����一����员�����char *tag_da
ta���,���嘶�占�枚�4���纸诘�指������,��������冉喜����:����一������时��
��������为struct pppoe_tag����一���存,��为tag_data�����存,�������头�时��
要�����头�tag_data占�玫��存,���头�pppoe_tag占�玫��存;������������时��直��
为struct pppoe_tag����一��struct pppoe_tag��小����tag_data���存,����一��42
0�锌��钥�����tag_data������要���谐�始����要��tag_data指��strct 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.����械��业�����,2004.6��82页����说����arr is defined to be identical 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]��0xfe��直��寻址��直�咏�0xfe写��&a[0]+2�牡�址����b[2]=0xfe�羌�
��寻址���冉�b�����荩���址���贸�������2����0xfe写���������牡�址������a[0]��b
[0]�������遣�同�摹�����������为����时����指���没������恕�
int do1(char a[],int len);
int do2(char *a,int len);
�����������械�a�����魏���稹6���实实���诖��诘�指�������

顺����说一�拢�����struct pppoe_tag�����一����员�亩�����char tag_data[0]��某
些��������支�殖���为0������亩��,����������拢�只�芙������char tag_data
[1]��使�梅�����同��

 
 
�� �� kkzhou (fdsa) �拇�����
�到: ��: ����一��指��: 指��一��ipv6��址: �� �� Coolb (小����) �拇������到: 
�� : :  69 struct rt0_hdr {
: :  70         struct ipv6_rt_hdr      rt_hdr;
: :  71         __u32                   bitmap;         /* strict/loose bit map 
: : */
: :  72         struct in6_addr         addr[0];
: :  73
: :  74 #define rt0_type                rt_hdr.type
: .................������省�裕�