1. attribute method:

    #include <stdio.h>
    
    struct packed
    {
    char a;
    int b;
    } __attribute__((packed)); struct not_packed
    {
    char a;
    int b;
    }; int main(void)
    {
    printf("Packed: %zu\n", sizeof(struct packed));
    printf("Not Packed: %zu\n", sizeof(struct not_packed));
    return 0;
    }

    Output:

    $ make example && ./example
    cc example.c -o example
    Packed: 5
    Not Packed: 8
  1. pragma pack method:

    #include <stdio.h>
    
    #pragma pack(1)
    struct packed
    {
    char a;
    int b;
    };
    #pragma pack() struct not_packed
    {
    char a;
    int b;
    }; int main(void)
    {
    printf("Packed: %zu\n", sizeof(struct packed));
    printf("Not Packed: %zu\n", sizeof(struct not_packed));
    return 0;
    }

    Output:

    $ make example && ./example
    cc example.c -o example
    Packed: 5
    Not Packed: 8
  2. Add -fpack-struct to GCC
    -fpack-struct[=n]
    Without a value specified, pack all structure members together without holes. When a value is specified (which must be a small power of two), pack structuremembers according to this value, representing the maximum alignment (that is, objects with default
    alignment requirements larger than this will be outputpotentially unaligned at the next fitting location.

    Warning: the -fpack-struct switch causes
    GCC to generate code that is not binary compatible with code generated without thatswitch. Additionally, it makes the code suboptimal. Use it to conform to a non-default application binary interface.

No Memory Alignment with GCC的更多相关文章

  1. 还是说Memory Model,gcc的__sync_synchronize真是太坑爹了

    还是说Memory Model,gcc的__sync_synchronize真是太坑爹了! 时间 2012-01-29 03:18:35  IT牛人博客聚合网站 原文  http://www.udpw ...

  2. 从硬件到语言,详解C++的内存对齐(memory alignment)

    转载请保留以下声明 作者:赵宗晟 出处:https://www.cnblogs.com/zhao-zongsheng/p/9099603.html 很多写C/C++的人都知道“内存对齐”的概念以及规则 ...

  3. 从硬件到语言,详解C++的内存对齐(memory alignment)(一)

    作者:赵宗晟 出处:https://www.cnblogs.com/zhao-zongsheng/p/9099603.html 很多写C/C++的人都知道“内存对齐”的概念以及规则,但不一定对他有很深 ...

  4. 编写跨平台代码之memory alignment

    编写网络包(存储在堆上)转换程序时,在hp-ux机器上运行时会遇到 si_code: 1 - BUS_ADRALN - Invalid address alignment. Please refer ...

  5. GNU C - 关于8086的内存访问机制以及内存对齐(memory alignment)

    一.为什么需要内存对齐? 无论做什么事情,我都习惯性的问自己:为什么我要去做这件事情? 是啊,这可能也是个大家都会去想的问题, 因为我们都不能稀里糊涂的或者.那为什么需要内存对齐呢?这要从cpu的内存 ...

  6. #define barrier() __asm__ __volatile__("": : :"memory") 中的memory是gcc的东西

    gcc内嵌汇编简介 在内嵌汇编中,可以将C语言表达式指定为汇编指令的操作数,而且不用去管如何将C语言表达式的值读入哪个寄存器,以及如何将计算结果写回C 变量,你只要告诉程序中C语言表达式与汇编指令操作 ...

  7. C++ Standards Support in GCC - GCC 对 C++ 标准的支持

    C++ Standards Support in GCC - 2019-2-20 GCC supports different dialects of C++, corresponding to th ...

  8. reds Virtual Memory

    Virtual Memory technical specification This document details the internals of the Redis Virtual Memo ...

  9. (C/C++) Interview in English. - Memory Allocation/Deallocation.

    Q: What is the difference between new/delete and malloc/free? A: Malloc/free do not know about const ...

随机推荐

  1. 资产管理软件 GLPI的安装(转)

     资产管理系统GLPI的安装 2010-07-05 16:20:31 标签:休闲 资产管理系统 GLPI 职场 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则 ...

  2. Appium切换webview时候报chromedriver版本问题

    前言 用appium切换webview的时候报chrome和chromedriver版本的问题:session not created exception: Chrome version must b ...

  3. java中传入一个数或字符串或数组进行反转

    //将一个数用递归反转--利用余数 public static void inverse(int n) { System.out.print(n % 10); if (n >= 10) inve ...

  4. 被忽视的控件UIToolbar

    前言 UIToolbar以前也接触过,不过没有怎么用,久而久之就忘了他的存在,今天看别人源码的时候看见了,它怎么很方便,在排列一排视图的时候不需要我们算里面的坐标就可以轻松良好的把布局做出来 代码 U ...

  5. 纯干货,Mysql innodb的ACID特性是怎么实现的?以及高并发情况下会出现的问题

    首先说说什么是ACID: 它们分别是Atomicity(原子性),Consistency(一致性),Isolation(隔离性),Transaction(持久性) 原子性: 意为单个事务里的多个操作要 ...

  6. 【HDOJ5978】To begin or not to begin(概率)

    题意:有k个黑球和1个红球,两个轮流抽,抽到红球算赢,问先手赢的概率大还是后手大还是相等 k<=1e5 思路:手算前几项概率 大胆猜想 #include<cstdio> #inclu ...

  7. Yii 之Session使用

    public function actionIndex(){ $session = \YII::$app->session; //判断session是否开启 if(!$session->i ...

  8. 记一次ORM的权衡和取舍

    面对ORM的选型,有些人是根据自己熟悉程度来评判,有些人是根据他人的推荐来抉择,有些人觉得都差不多,随便了.当自己要真正做选择的时候,以上的这些依据都无法真正说服自己,因为不同的业务需求,不同的团队构 ...

  9. STM32F10x_StdPeriph_Driver_3.5.0(中文版).chm的使用

    以熟悉的固件库函数说明中函数GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_IintTypeDef *GPIO_InitStructure)为例 GPIOA...G       ...

  10. CentOS6、CentOS7配置Base源和epel源

    1.用yum安装软件报错 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&a ...