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. Knockout v3.4.0 中文版教程-4-通过监控数组工作

    2.通过监控数组工作 1. 监控数组 如果你想检测或者响应一个对象的改变,你用observables.如果你想检测和响应一个集合的改变,使用observableArray.这个在很多情况下都非常有用, ...

  2. [uiautomator篇]uiwatcher 的使用场景

    http://www.yangyanxing.com/article/use-watcher-in-uiautomator.html   今天在uiautomator中实践了watcher的用法,这个 ...

  3. TOJ 4244: Sum

    4244: Sum   Time Limit(Common/Java):3000MS/9000MS     Memory Limit:65536KByteTotal Submit: 63       ...

  4. iOS学习笔记19-地图(一)定位CoreLocation

    一.定位介绍 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.要实现 ...

  5. 【bzoj1059】[ZJOI2007]矩阵游戏 二分图最大匹配

    题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换 ...

  6. 【Luogu】P1578奶牛浴场(DP,枚举)

    题目链接 枚举极大子矩形.详情请见本题题解:I_AM_HelloWord 代码如下 #include<cstdio> #include<cctype> #include< ...

  7. BZOJ 3240 [Noi2013]矩阵游戏 ——费马小定理 快速幂

    发现是一个快速幂,然而过不去. 怎么办呢? 1.十进制快速幂,可以用来练习卡时. 2.费马小定理,如果需要乘方的地方,可以先%(p-1)再计算,其他地方需要%p,所以需要保存两个数. 然后就是分类讨论 ...

  8. 16.1116 NOIP 考前模拟(信心题)

    分火腿 (hdogs.pas/.c/.cpp) 时间限制:1s:内存限制 64MB 题目描述: 小月言要过四岁生日了,她的妈妈为她准备了n根火腿,她想将这些火腿均分给m位小朋友,所以她可能需要切火腿. ...

  9. PSEUDO LEAST RECENTLY USED (PLRU) CACHE REPLACEMENT

    A multi-way cache system includes multi-way cache storage circuitry, a pseudo least recently used (P ...

  10. Snoop resynchronization mechanism to preserve read ordering

    A processor employing a post-cache (LS2) buffer. Loads are stored into the LS2buffer after probing t ...