zoukankan      html  css  js  c++  java
  • __attribute__ 变量对齐

    http://blog.163.com/sunm_lin/blog/static/9192142200741533038695/

    一.   __attribute__ ((aligned (n)))

    该属性规定变量或结构体成员的最小的对齐格式,以字节为单位。例如:
    int x __attribute__ ((aligned (16))) = 0;
    编译器将以16字节(注意是字节byte不是位bit)对齐的方式分配一个变量。也可以对结构体成员变量设置该属性

    二、__attribute__ ((aligned))

    选择针对目标机器最大的对齐方式,可以提高拷贝操作的效率

    三、__attribute__ ((packed))

    使用该属性可以使得变量或者结构体成员使用最小的对齐方式,即对变量是一字节对齐,对域(field)是位对齐。

    四、例子

    struct foo {   char a;   int x[4] __attribute__ ((packed));   };//17
    struct bar8{   char a;   int x[4] __attribute__ ((aligned(8)));   };//24
    struct bar16 {   char a;   int x[4] __attribute__ ((aligned(16)));   };//32
    struct bar32{  char a;  int x[4] __attribute__ ((aligned(32)));   };//64
    struct bar {   //char a;   char x[4] __attribute__ ((aligned));   };
    struct bar_16 {   char a;   int x[4] __attribute__ ((aligned));   };
    [root@mip-123456 attribute]# ./var_attribute

    sizeof(foo)=17

    sizeof(bar)=16

    sizeof(bar_16)=32

    sizeof(bar8)=24

    sizeof(bar16)=32

    sizeof(bar32)=64

  • 相关阅读:
    SpringDataJpa
    #pragma pack(n)的使用
    jquery中的ajax方法参数
    rapidjson的使用
    Linux下Qt安装
    jsoncpp 0.5 ARM移植
    GoAhead2.5移植到ARM教程
    Qt 4.7.2移植到ARM教程
    虚函数与纯虚函数的区别
    海康、大华IpCamera RTSP地址和格式
  • 原文地址:https://www.cnblogs.com/chencesc/p/5728476.html
Copyright © 2011-2022 走看看