Evans_Y 2023-03-19 21:13 采纳率: 50%
浏览 31

C语言 __attribute__ ((packed)) 使用

C语言使用__attribute__ ((packed))
测试的struct 占用字节大小感到挺奇怪, 不能理解 (详细见图)

软件: Dev 5.6.3
编译: TDM-GCC 4.8.1 32

源代码:

#include <stdio.h>

typedef struct 
{
    char A; //1
    int B; //4
    char C; //1
    double D; //8
    char E; //1

}__attribute__ ((packed)) StructA;

struct StructB
{
    char A;
    int B;
    char C;
    double D;
    char E;
}; 

int main(void)
{

    StructA StructAExample;
    struct StructB StructBExample;
    
    printf("short 占用的字节 %d \n", sizeof(short));
    printf("int 占用的字节 %d \n", sizeof(int));
    printf("double 占用的字节 %d \n", sizeof(double));
    printf("StructA 占用的字节 %d \n", sizeof(StructAExample));
    printf("StructB 占用的字节 %d \n", sizeof(StructBExample));

    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-03-19 21:32
    关注
    评论

报告相同问题?

问题事件

  • 创建了问题 3月19日

悬赏问题

  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画
  • ¥15 TeleScan不能修改bar
  • ¥100 请问我基于逐飞库写的这个有关于mp u6050传感器的函数,为什么输出的值是固定的?
  • ¥15 hadoop中启动hive报错如下怎么解决
  • ¥15 如何优化QWebEngineView 加载url的速度
  • ¥15 关于#hadoop#的问题,请各位专家解答!
  • ¥15 如何批量抓取网站信息
  • ¥15 Spring Boot离线人脸识别