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

        <bdo id='Kirp9'></bdo><ul id='Kirp9'></ul>
    1. <small id='Kirp9'></small><noframes id='Kirp9'>

      强制 C++ 结构紧密包装

      时间:2023-07-01
      <legend id='zXED9'><style id='zXED9'><dir id='zXED9'><q id='zXED9'></q></dir></style></legend>
        <bdo id='zXED9'></bdo><ul id='zXED9'></ul>
          <tbody id='zXED9'></tbody>

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

            1. <small id='zXED9'></small><noframes id='zXED9'>

                本文介绍了强制 C++ 结构紧密包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试读取二进制文件.问题是文件的创建者没有花时间将数据结构正确对齐到它们的自然边界,并且所有内容都被打包得很紧.这使得使用 C++ 结构读取数据变得困难.

                I am attempting to read in a binary file. The problem is that the creator of the file took no time to properly align data structures to their natural boundaries and everything is packed tight. This makes it difficult to read the data using C++ structs.

                有没有办法强制 struct 被压缩?

                Is there a way to force a struct to be packed tight?

                示例:

                struct {
                    short a;
                    int b;
                }
                

                上述结构为8个字节:short a为2个,padding为2个,int b为4个.但是,在磁盘上,数据只有 6 个字节(没有 2 个字节的填充对齐)

                The above structure is 8 bytes: 2 for short a, 2 for padding, 4 for int b. However, on disk, the data is only 6 bytes (not having the 2 bytes of padding for alignment)

                请注意实际的数据结构是数千字节和许多字段,包括几个数组,所以我不想单独读取每个字段.

                Please be aware the actual data structures are thousands of bytes and many fields, including a couple arrays, so I would prefer not to read each field individually.

                推荐答案

                如果你使用 GCC,你可以做 struct __attribute__ ((packed)) { short a;国际b;}

                If you're using GCC, you can do struct __attribute__ ((packed)) { short a; int b; }

                在 VC++ 上,您可以执行 #pragma pack(1).此选项GCC 也支持.

                On VC++ you can do #pragma pack(1). This option is also supported by GCC.

                #pragma pack(push, 1)
                struct { short a; int b; }
                #pragma pack(pop)
                

                其他编译器可能会选择在没有填充的情况下对结构进行紧密打包.

                Other compilers may have options to do a tight packing of the structure with no padding.

                这篇关于强制 C++ 结构紧密包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何将字符串转换为 IP 地址,反之亦然 下一篇:在 C++ 中的变量声明中使用 struct 关键字

                相关文章

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

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

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