zoukankan      html  css  js  c++  java
  • No Memory Alignment with GCC

    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
      ", sizeof(struct packed));
          printf("Not Packed: %zu
      ", 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
      ", sizeof(struct packed));
          printf("Not Packed: %zu
      ", 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.



  • 相关阅读:
    WinMain函数的修饰符WINAPI的含义
    java字节码指令集简介
    vs2010里面的ipch文件和.sdf文件是什么
    java查看class字节码文件
    从汇编看c++的extern关键字
    highcharts系列教程
    highcharts的文档介绍(英文)
    关于firebug中行号和源文件不一致的问题
    ios中的流状态的定义
    highcharts翻译系列
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7201795.html
Copyright © 2011-2022 走看看