zoukankan      html  css  js  c++  java
  • struct深层解析

    一、struct的巨大作用

    面对一个人的大型C/C++程序时,只看其对struct的使用情况我们就可以对其编写者的编程经验进行评估。因为一个大型的C/C++程序,势必要涉及一些(甚至大量)进行数据组合的结构体,这些结构体可以将原本意义属于一个整体的数据组合在一起。从某种程度上来说,会不会用struct,怎样用struct是区别一个开发人员是否具备丰富开发经历的标志
    在网络协议、通信控制、嵌入式系统的C/C++编程中,我们经常要传送的不是简单的字节流(char型数组),而是多种数据组合起来的一个整体,其表现形式是一个结构体。

      经验不足的开发人员往往将所有需要传送的内容依顺序保存在char型数组中,通过指针偏移的方法传送网络报文等信息。这样做编程复杂,易出错,而且一旦控制方式及通信协议有所变化,程序就要进行非常细致的修改。

      一个有经验的开发者则灵活运用结构体,举一个例子,假设网络或控制协议中需要传送三种报文,其格式分别为

    C代码 复制代码 收藏代码
    1. struct structA    
    2. {   
    3.    int a;   
    4.    char b;   
    5. } ;   
    6.   
    7. struct structB    
    8. {   
    9.    char a;   
    10.    short b;   
    11. } ;   
    12.   
    13. struct structC   
    14. {   
    15.    int a;   
    16.    char b;   
    17.    float c;   
    18. } ;  
    struct structA 
    {
       int a;
       char b;
    } ;
    
    struct structB 
    {
       char a;
       short b;
    } ;
    
    struct structC
    {
       int a;
       char b;
       float c;
    } ;

    优秀的程序设计者这样设计传送的报文:

    C代码 复制代码 收藏代码
    1. struct CommuPacket   
    2. {   
    3.    int iPacketType;   // 报文类型标志    
    4.   union       // 每次传送的是三种报文中的一种,使用union    
    5.    {   
    6.      struct structA packetA;   
    7.      struct structB packetB;   
    8.      struct structC packetC;   
    9.    }    
    10. } ;  
    struct CommuPacket
    {
       int iPacketType;   // 报文类型标志 
      union       // 每次传送的是三种报文中的一种,使用union 
       {
         struct structA packetA;
         struct structB packetB;
         struct structC packetC;
       } 
    } ;

    在进行报文传送时,直接传送struct CommuPacket一个整体。

    假设发送函数的原形如下:

    // pSendData:发送字节流的首地址,iLen:要发送的长度
    Send( char * pSendData, unsigned int    iLen);
    发送方可以直接进行如下调用发送struct CommuPacket的一个实例sendCommuPacket:
    Send( (
    char * ) & sendCommuPacket , sizeof (CommuPacket) );
    假设接收函数的原形如下:
    // pRecvData:发送字节流的首地址,iLen:要接收的长度
    // 返回值:实际接收到的字节数
    unsigned int Recv( char * pRecvData, unsigned int    iLen);

    接收方可以直接进行如下调用将接收到的数据保存在struct CommuPacket的一个实例recvCommuPacket中:

    Recv( ( char * ) & recvCommuPacket , sizeof (CommuPacket) );

    接着判断报文类型进行相应处理:

    C代码 复制代码 收藏代码
    1. switch (recvCommuPacket. iPacketType)   
    2.  {   
    3.     case PACKET_A:   
    4.     …     // A类报文处理    
    5.      break ;   
    6.     case PACKET_B:   
    7.     …    // B类报文处理    
    8.      break ;   
    9.     case PACKET_C:   
    10.     …    // C类报文处理    
    11.      break ;   
    12.    
     switch (recvCommuPacket. iPacketType)
      {
         case PACKET_A:
         …     // A类报文处理 
          break ;
         case PACKET_B:
         …    // B类报文处理 
          break ;
         case PACKET_C:
         …    // C类报文处理 
          break ;
    } 

     以上程序中最值得注意的是:

    C代码 复制代码 收藏代码
    1. Send( (char *)&sendCommuPacket , sizeof(CommuPacket) );   
    2. Recv( (char *)&recvCommuPacket , sizeof(CommuPacket) );  
    Send( (char *)&sendCommuPacket , sizeof(CommuPacket) );
    Recv( (char *)&recvCommuPacket , sizeof(CommuPacket) );

    中的强制类型转换:(char *)&sendCommuPacket、(char *)&recvCommuPacket,先取地址,再转化为char型指针,这样就可以直接利用处理字节流的函数。

      利用这种强制类型转化,我们还可以方便程序的编写,例如要对sendCommuPacket所处内存初始化为0,可以这样调用标准库函数memset():

     

    memset((char *)&sendCommuPacket,0, sizeof(CommuPacket));

     二、struct的成员对齐

     

    Cpp代码 复制代码 收藏代码
    1. 1 . #include < iostream.h >    
    2.   
    3. 2 . #pragma pack( 8 )   
    4. 3 . struct example1   
    5. 4 .  {   
    6. 5 . short a;   
    7. 6 . long b;   
    8. 7 . } ;   
    9.   
    10. 8 . struct example2   
    11. 9 .  {   
    12. 10 . char c;   
    13. 11 . example1 struct1;   
    14. 12 . short e;       
    15. 13 . } ;   
    16. 14 . #pragma pack()   
    17.   
    18. 15 . int main( int argc, char * argv[])   
    19. 16 .  {   
    20. 17 . example2 struct2;   
    21.   
    22. 18 . cout << sizeof (example1) << endl;   
    23. 19 . cout << sizeof (example2) << endl;   
    24. 20 . cout << (unsigned int )( & struct2.struct1) - (unsigned int )( & struct2)    
    25. << endl;   
    26.   
    27. 21 . return 0 ;   
    28. 22 . }   
     1 . #include < iostream.h > 
     
     2 . #pragma pack( 8 )
     3 . struct example1
     4 .  {
     5 . short a;
     6 . long b;
     7 . } ;
    
     8 . struct example2
     9 .  {
     10 . char c;
     11 . example1 struct1;
     12 . short e;    
     13 . } ;
     14 . #pragma pack()
    
     15 . int main( int argc, char * argv[])
     16 .  {
     17 . example2 struct2;
    
     18 . cout << sizeof (example1) << endl;
     19 . cout << sizeof (example2) << endl;
     20 . cout << (unsigned int )( & struct2.struct1) - (unsigned int )( & struct2) 
     << endl;
    
     21 . return 0 ;
     22 . } 

      

    问程序的输入结果是什么?

    答案是:

    8
    16
    4

    不明白?还是不明白?下面一一道来:

    1) 自然对界

      struct是一种复合数据类型,其构成元素既可以是基本数据类型(如int、long、float等)的变量,也可以是一些复合数据类型(如array、struct、union等)的数据单元。对于结构体,编译器会自动进行成员变量的对齐,以提高运算效率。缺省情况下,编译器为结构体的每个成员按其自然对界(natural alignment)条件分配空间。各个成员按照它们被声明的顺序在内存中顺序存储,第一个成员的地址和整个结构的地址相同。

      自然对界(natural alignment)即默认对齐方式,是指按结构体的成员中size最大的成员对齐。

    例如:

    C代码 复制代码 收藏代码
    1. struct naturalalign   
    2. {   
    3.    char a;   
    4.    short b;    
    5.    char c;   
    6. };  
    struct naturalalign
    {
       char a;
       short b; 
       char c;
    };

     在上述结构体中,size最大的是short,其长度为2字节,因而结构体中的char成员a、c都以2为单位对齐,sizeof(naturalalign)的结果等于6;

     

    如果改为:

    C代码 复制代码 收藏代码
    1. struct naturalalign   
    2. {   
    3.    char a;   
    4.    int b;   
    5.    char c;   
    6. };  
    struct naturalalign
    {
       char a;
       int b;
       char c;
    };

    其结果显然为12。

    2) 指定对界

    一般地,可以通过下面的方法来改变缺省的对界条件:

      · 使用伪指令#pragma pack (n),编译器将按照n个字节对齐;
      · 使用伪指令#pragma pack (),取消自定义字节对齐方式。

      注意:如果#pragma pack (n)中指定的n大于结构体中最大成员的size,则其不起作用,结构体仍然按照size最大的成员进行对界。

    例如:

    C代码 复制代码 收藏代码
    1. #pragma pack (n)   
    2. struct naturalalign   
    3. {   
    4.    char a;   
    5.    int b;   
    6.    char c;   
    7. };   
    8. #pragma pack ()  
    #pragma pack (n)
    struct naturalalign
    {
       char a;
       int b;
       char c;
    };
    #pragma pack ()

    当n为4、8、16时,其对齐方式均一样,sizeof(naturalalign)的结果都等于12。而当n为2时,其发挥了作用,使得sizeof(naturalalign)的结果为8。

      在VC++ 6.0编译器中,我们可以指定其对界方式(见图),其操作方式为依次选择projetct > setting > C/C++菜单,在struct member alignment中指定你要的对界方式。

     

    另外,通过__attribute((aligned (n)))也可以让所作用的结构体成员对齐在n字节边界上,但是它较少被使用,因而不作详细讲解。

    3) 面试题的解答

          至此,我们可以对Intel、微软的面试题进行全面的解答:

      程序中第2行#pragma pack (8)虽然指定了对界为8,但是由于struct example1中的成员最大size为4(long变量size为4),故struct example1仍然按4字节对界,struct example1的size为8,即第18行的输出结果;

      struct example2中包含了struct example1,其本身包含的简单数据成员的最大size为2(short变量e),但是因为其包含了struct example1,而struct example1中的最大成员size为4,struct example2也应以4对界,#pragma pack (8)中指定的对界对struct example2也不起作用,故19行的输出结果为16;

      由于struct example2中的成员以4为单位对界,故其char变量c后应补充3个空,其后才是成员struct1的内存空间,20行的输出结果为4。

  • 相关阅读:
    好用,Office超效率速成技
    Oracle 12c从入门到精通:视频教学超值版
    用Excel学数据分析
    VMware、Citrix和Microsoft虚拟化技术详解与应用实践
    中文版Dreamweaver CC+Flash CC+Photoshop CC网页设计基础培训教程(新编实战型全功能培训教材)
    1467.二叉排序树
    1177.查找
    1178.复数集合
    1165.字符串匹配
    1166.迭代求立方根
  • 原文地址:https://www.cnblogs.com/lzlsky/p/2662378.html
Copyright © 2011-2022 走看看