简体   繁体   English

结构对齐和通过指向32bit的指针访问两个16bit字段

[英]Struct Alignment & Access two 16bit fields via pointer to 32bit

I a problem on struct alignment. 我在结构对齐上遇到问题。 It seems no matter what I do the compiler inserts a byte between the two fields of the struct. 看来不管我做什么,编译器都会在结构的两个字段之间插入一个字节。 This is a sample of the output 这是输出的样本

 4  +j 4    +++       40004    .........    4 
 5  +j 5    +++       50005    .........    5 
 6  +j 6    +++       60006    .........    6 
 7  +j 7    +++       70007    .........    7 
 8  +j 8    +++       80008    .........    8 
 9  +j 9    +++       90009    .........    9 

The byte 00 is inserted between the re and im fields of the H struct. 字节00插入在H结构的re和im字段之间。 How I can stop the compiler doing this to H so that so that the pointer pW can read both fields as 32 bit through the pointer pW? 如何停止编译器对H进行操作,以便指针pW可以通过指针pW读取32位的两个字段?

Maybe I need to change the size of the 3d-array. 也许我需要更改3d数组的大小。 If there is a way without changing the array size would be great. 如果有一种方法不更改数组大小,那就太好了。

#include <stdio.h>
#include <stdlib.h>

#define NA 4
#define NS 3
#define NF 5

typedef struct  {
  short re;
  short im;
} cint16 ;

typedef struct 
{
   cint16   H[NRx][NSTS][NFFT];
} AAA;

AAA        H;
AAA *      pH = &H;

int main(void)
{
    int i, j, k, n, m;
    cint16 *    pC;
    int *       pW;

    n = 0;
    for(i=0; i<NA; i++)
    {
        for(j=0; j<NS; j++)
        {
            for(k=0; k<NF; k++)
            {  
                H.H[i][j][k].re = n ;
                H.H[i][j][k].im = n;

                n++;
            }
        }
    }

    pC = &H.H[0][0][0];
    m = 0;
    for(k=0; k<NA; k++)
    {
        for(i=0; i<NS; i++)
        {
            for(n=0; n<NF; n++)
            {
                printf("%02d  ",    pC[m].re );
                printf("+j%02d,",   pC[m].im );
                printf("     ");
                m++;
            }
            printf("\n"); 
        }    
    }

    printf("\n\n");

    pW = (int *)&H.H[0][0][0];
    pC = &H.H[0][0][0];
    m = 0;
    for(k=0; k<NA*NS*NF; k++)
    {
        printf("%2X  ",   pC[m].re );
        printf("+j%2X",   pC[m].im );
        printf("    +++       ");
        printf("%X  ",   pW[m] );

        printf("  .........    %d \n", m);

        m++;
    }


    exit (0);
}

You misinterpret the output. 您误解了输出。

printf("%X  ",   pW[m] );

prints the four bytes of the struct as an unsigned int in hexadecimal representation 以十六进制表示形式将结构的四个字节打印为unsigned int

    4|00|04

The first 4 is from the one non-zero byte of the struct member corresponding to the higher-order bytes of the unsigned int (whether that's re or im depends on endianness), the next two bytes, 00 and 04 are from the two bytes of the other member. 4来自struct成员的一个非零字节,该字节对应于unsigned int的高位字节(无论是re还是im取决于字节序),接下来的两个字节0004来自这两个字节其他成员的。

There is no byte inserted between the members, there is one byte (and one nibble) not printed out due to the suppression of leading zeros. 成员之间没有插入字节,由于抑制了前导零,所以没有打印出一个字节(和一个半字节)。

It depends on what compiler you are using. 这取决于您使用的编译器。 On GCC there is __attribute__ ((__packed__)) , on VC++ there is #pragma pack . 在GCC上有__attribute__ ((__packed__)) ,在VC ++上有#pragma pack

There are probably many duplicate questions here on SO that shows how to use those. SO上可能有很多重复的问题,这些问题说明了如何使用这些问题。


And a slight note of warning: Using a 32-bit integer to access two 16-bit values will depend much on the byte-ordering of the underlying platform. 还有一点警告:使用32位整数访问两个16位值将在很大程度上取决于基础平台的字节顺序。 If you eg send this structure over the Internet or by file, you have to make sure you convert it properly. 例如,如果通过Internet或通过文件发送此结构,则必须确保正确转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM