自定义内存对齐方式

/*
 * =====================================================================================
 *
 *       Filename:  alignment_demo.c
 *
 *    Description:  alignment demo
 *
 *        Version:  1.0
 *        Created:  07/27/2012 11:34:37 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  TED (gk), guolb57@163.com
 *
 * =====================================================================================
 */

#include <stdio.h>

struct tag_test_a{
    int l;
    short i;
    char c;	
};
typedef struct tag_test_a test_a, *ptest_a;


struct tag_test_b{
    short i;
    int l;
    char c;	
};
typedef struct tag_test_b test_b, *ptest_b;


/*属性aligned用于变量、结构或者联合,设定一个指定大小的对齐格式,以字节为单位。
 * 但是要注意,这个属性的设定只能用来增加对齐地址而不能减少,例如:
 *        char c __attribute__((aligned(16)));    //让变量 c 以16字节对齐。
 *        int i __attribute__((aligned(2)));      //该aligned属性想要设定变量 i 以2字节对齐,
 *                                                //但由于 i 需要4字节对齐。因此该声明无效
*/
struct tag_test_c{
    int l;
    short i;
    char c;	
}__attribute__((aligned(8)));  /* should be power of 2, 
                                  and bigger size of the longest member of structure(here is l) */
typedef struct tag_test_c test_c, *ptest_c;

struct tag_test_d{
    short i;
    int l;
    char c;	
}__attribute__((aligned(8)));
typedef struct tag_test_d test_d, *ptest_d;

struct tag_test_e{
    short i;
    int l;
    char c;	
}__attribute__((__packed__));
typedef struct tag_test_e test_e, *ptest_e;

struct tag_test_f{
    short i;
    int l;
    char c;	
}__attribute__((__packed__));
typedef struct tag_test_f test_f, *ptest_f;

int main(int argc, const char *argv[])
{
    /* ********************************************************************** */
    /*gcc alignment_demo.c ; ./a.out
     * the out put: 
     * sizeof(test_a) = 8
     * sizeof(test_b) = 12
     * after aligment:
     * sizeof(test_c) = 8
     * sizeof(test_d) = 16
     * after packed:
     * sizeof(test_e) = 7
     * sizeof(test_f) = 7 */
    /* ********************************************************************** */
    printf("sizeof(test_a) = %d\n", sizeof(test_a));
    printf("sizeof(test_b) = %d\n", sizeof(test_b));
    /* ********************************************************************** */
    printf("after aligment:\n");
    printf("sizeof(test_c) = %d\n", sizeof(test_c));
    printf("sizeof(test_d) = %d\n", sizeof(test_d));
    /* ********************************************************************** */
    printf("after packed:\n");
    printf("sizeof(test_e) = %d\n", sizeof(test_e));
    printf("sizeof(test_f) = %d\n", sizeof(test_f));
    /* ********************************************************************** */

    return 0;
}

注意:1. 在Linux下:在GNU扩展一文中,aligned(ALIGNMENT)的作用是对数据强制对其:

           属性aligned用于变量、结构或者联合,设定一个指定大小的对齐格式,以字节为单位。但是要注意,这个属性的设定只能用来增加对齐地址而不能减少,例如:

                           char c __attribute__((aligned(16)));              //让变量 c 以16字节对齐。

                           int i __attribute__((aligned(2)));                    //该aligned属性想要设定变量 i 以2字节对齐,但由于 i 需要4字节对齐。因此该声明无效。

             2. 在VC环境下,一般地,可以通过下面的方法来改变缺省的对齐条件:

                               · 使用伪指令#pragma pack(n),编译器将按照n个字节对齐;

                               · 使用伪指令#pragma pack(),取消自定义字节对齐方式。

            3.  注意:如果#pragma pack (n)中指定的n大于结构体中最大成员(类型)的size,则其不起作用,结构体仍然按照size最大的成员进行对齐。

            4. 在VC++ 6.0编译器中,我们可以指定其对齐方式,其操作方式为依次选择projetct > setting > C/C++菜单,在struct member alignment中指定你要的对其方式。

            5. 使用packed,仅在你需要的时候。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值