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. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  2. Centos7 安装配置优化mysql(mariadb分支)

    1.说明 由于在centos7的yum仓库中没有mysql,centos7用mariadb替代了mysql. mariadb是mysql源代码的一个分支, mysql被ORACLE闭源,而mariad ...

  3. Quartz与Terracotta的集成

    Quartz与Terracotta的集成 博客分类: Java&Socket 企业应用面临的问题 开源组件的应用 quartz任务集群数据库任务调度Terracotta  Quartz与Ter ...

  4. Android单个按钮自定义Dialog

    代码改变世界 Android单个按钮自定义Dialog dialog_layout.xml <?xml version="1.0" encoding="utf-8& ...

  5. 转载 :sql server 2005 无法删除数据库 "#Test",因为该数据库当前正在使用

    无法删除数据库 "#Test",因为该数据库当前正在使用 --查询分析器中执行下面的语句就行了. use master go declare @dbname sysname set ...

  6. android悬浮窗口的一些说明

    1.xml文件里的权限申请 <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" ...

  7. Vxlan简介

    1.为什么需要Vxlan 1.什么是VXLAN VXLAN(Virtual eXtensible LAN可扩展虚拟局域网),是一种mac in UDP技术.传统的二层帧被封装到了UDP的报文中,通过U ...

  8. 关于EOF,转自新浪微博

    本文转自http://blog.sina.com.cn/s/blog_7714171f0101798y.html EOF 是 End Of File 的缩写. 在C语言中,它是在标准库中定义的一个宏. ...

  9. OC-Runtime温故知新

    每个java应用程序都有一个runtime类实例,使应用程序能够与其运行的环境相连接.可以通过getRuntime 方法获取当前运行时,应用程序不能自己创建runtime类实例.Runtime 没有构 ...

  10. TCP/IP 协议栈

    TCP(传输控制协议) 传输控制协议(Transmission Control Protocol,TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义. 在因特 ...