<small id='LS8eu'></small><noframes id='LS8eu'>

  1. <legend id='LS8eu'><style id='LS8eu'><dir id='LS8eu'><q id='LS8eu'></q></dir></style></legend>
  2. <tfoot id='LS8eu'></tfoot>
    <i id='LS8eu'><tr id='LS8eu'><dt id='LS8eu'><q id='LS8eu'><span id='LS8eu'><b id='LS8eu'><form id='LS8eu'><ins id='LS8eu'></ins><ul id='LS8eu'></ul><sub id='LS8eu'></sub></form><legend id='LS8eu'></legend><bdo id='LS8eu'><pre id='LS8eu'><center id='LS8eu'></center></pre></bdo></b><th id='LS8eu'></th></span></q></dt></tr></i><div id='LS8eu'><tfoot id='LS8eu'></tfoot><dl id='LS8eu'><fieldset id='LS8eu'></fieldset></dl></div>

      <bdo id='LS8eu'></bdo><ul id='LS8eu'></ul>

    1. C 结构中的内存对齐

      时间:2023-09-16

    2. <legend id='zv51b'><style id='zv51b'><dir id='zv51b'><q id='zv51b'></q></dir></style></legend>

            <i id='zv51b'><tr id='zv51b'><dt id='zv51b'><q id='zv51b'><span id='zv51b'><b id='zv51b'><form id='zv51b'><ins id='zv51b'></ins><ul id='zv51b'></ul><sub id='zv51b'></sub></form><legend id='zv51b'></legend><bdo id='zv51b'><pre id='zv51b'><center id='zv51b'></center></pre></bdo></b><th id='zv51b'></th></span></q></dt></tr></i><div id='zv51b'><tfoot id='zv51b'></tfoot><dl id='zv51b'><fieldset id='zv51b'></fieldset></dl></div>

          1. <tfoot id='zv51b'></tfoot>
                <tbody id='zv51b'></tbody>
            1. <small id='zv51b'></small><noframes id='zv51b'>

              • <bdo id='zv51b'></bdo><ul id='zv51b'></ul>
                本文介绍了C 结构中的内存对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在 32 位机器上工作,所以我想内存对齐应该是 4 个字节.假设我有这个结构:

                I'm working on a 32-bit machine, so I suppose that the memory alignment should be 4 bytes. Say I have this struct:

                typedef struct {
                    unsigned short v1;
                    unsigned short v2;
                    unsigned short v3;
                } myStruct;
                

                普通添加的大小是 6 个字节,我想对齐的大小应该是 8,但是 sizeof(myStruct) 返回我 6.

                The plain added size is 6 bytes, and I suppose that the aligned size should be 8, but sizeof(myStruct) returns me 6.

                但是如果我写:

                typedef struct {
                    unsigned short v1;
                    unsigned short v2;
                    unsigned short v3;
                    int i;
                } myStruct;
                

                普通添加的大小是10个字节,对齐的大小应该是12,这次sizeof(myStruct) == 12.

                the plain added size is 10 bytes, aligned size shall be 12, and this time sizeof(myStruct) == 12.

                有人能解释一下有什么区别吗?

                Can somebody explain what is the difference?

                推荐答案

                至少在大多数机器上,类型只与类型本身一样大的边界对齐.在您的实现中,short 显然是 2 个字节,而 int 是 4 个字节.

                At least on most machines, a type is only ever aligned to a boundary as large as the type itself . On your implementation, short is apparently 2 bytes, and int 4 bytes.

                这意味着您的第一个结构与 2 字节边界对齐.由于所有成员各占2个字节,因此它们之间没有插入填充.

                That means your first struct is aligned to a 2-byte boundary. Since all the members are 2 bytes apiece, no padding is inserted between them.

                第二个包含一个 4 字节的项目,它与 4 字节的边界对齐.由于前面是6个字节,所以在v3i之间插入了2个字节的padding,在shorts中给出了6个字节的数据,两个填充字节,以及 int 中另外 4 个字节的数据,总共 12 个.

                The second contains a 4-byte item, which gets aligned to a 4-byte boundary. Since it's preceded by 6 bytes, 2 bytes of padding is inserted between v3 and i, giving 6 bytes of data in the shorts, two bytes of padding, and 4 more bytes of data in the int for a total of 12.

                这篇关于C 结构中的内存对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 C/C++ 中读/写任意位 下一篇:类/结构成员是否总是按照声明的顺序在内存中创

                相关文章

                最新文章

                    <small id='qWlLT'></small><noframes id='qWlLT'>

                    <i id='qWlLT'><tr id='qWlLT'><dt id='qWlLT'><q id='qWlLT'><span id='qWlLT'><b id='qWlLT'><form id='qWlLT'><ins id='qWlLT'></ins><ul id='qWlLT'></ul><sub id='qWlLT'></sub></form><legend id='qWlLT'></legend><bdo id='qWlLT'><pre id='qWlLT'><center id='qWlLT'></center></pre></bdo></b><th id='qWlLT'></th></span></q></dt></tr></i><div id='qWlLT'><tfoot id='qWlLT'></tfoot><dl id='qWlLT'><fieldset id='qWlLT'></fieldset></dl></div>

                  1. <tfoot id='qWlLT'></tfoot>

                  2. <legend id='qWlLT'><style id='qWlLT'><dir id='qWlLT'><q id='qWlLT'></q></dir></style></legend>
                    • <bdo id='qWlLT'></bdo><ul id='qWlLT'></ul>