【问题标题】:Why is the size of this structure 16 Byte?为什么这个结构的大小是 16 Byte?
【发布时间】:2019-02-03 14:24:14
【问题描述】:

BITMAPFILEHEADER 的大小应该是 14 个字节,但是当我运行代码时,我得到了 16 个字节。我想,这是因为填充,所以我添加了__attribute__((packed)),但我仍然得到 16 个字节。为什么?

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

typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;


typedef struct
{
WORD bfType;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfSize;
DWORD bfOffBits;
} __attribute__((packed))
BITMAPFILEHEADER;

int main(void){

printf(" %i \n",sizeof(BITMAPFILEHEADER));
return 0;


}

【问题讨论】:

  • 你用的是什么编译器?有哪些选择?
  • 我正在使用代码块。
  • 虽然您肯定会得到答案,但请考虑替代更高级别的编码目标并形成不需要像__attribute__((packed)) 这样的编译器扩展的解决方案。 BITMAPFILEHEADER 通常是填充问题。
  • code::blocks 是 IDE,而不是编译器。它支持许多不同的编译器,而你使用哪一个可能很重要。
  • @Maherkassem 什么版本,以及如何准确地调用它?

标签: c bitmap size padding


【解决方案1】:

在 GCC 4.8.1 中报告了相同问题的错误。

attribute((packed)) does not pack struct containing uint16_t with uint32_t

对该错误报告的评论指出:

(attribute((packed)) 不适用于 mingw32 目标,因为 ms-bitfields 已成为默认值)。

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 2014-12-29
    • 2021-12-18
    • 1970-01-01
    • 2013-11-19
    • 2010-11-08
    • 2011-01-27
    • 2015-06-05
    • 2013-03-26
    相关资源
    最近更新 更多