字节对齐陷阱(Alignment trap)-demo for ARM9

本文通过示例代码展示了在ARM926EJ-S处理器上遇到的对齐陷阱问题。总结了四条建议:使用32位变量、谨慎使用类型转换、清除警告以及不确定如何修复警告时咨询他人。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * =====================================================================================
 *
 *       Filename:  alignment_trap_demo.c
 *
 *    Description:  alignment trap demo
 *                 test on
 *                      Processor:  ARM926EJ-S   
 *                      HardWare: DaVinci DM6467 EVM
 *                  compile with:
 *                      arm_v5t_le-gcc -Wall alignment_trap_demo.c -o alignment_trap_demo 
 *                  reference:
 *                      http://e2e.ti.com/support/embedded/linux/f/354/t/41140.aspx
 *                      http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/
 *                  summary:
 *                  1. Try to use 32-bit variables.
 *                  Short and Char variables do not save memory or perform faster anyway
 *                  because the CPU loads them into 32-bit registers in any case, 
 *                  and has even more work to deal with them correctly.
 *                  2. Use casting with caution. Beware of arrays of bytes; 
 *                  don’t try to access this array with a 32-bit pointer.
 *                  3. Clean the warnings. Don’t leave warnings in your code.
 *                  4. Don’t fix warnings that you are not sure how to fix. Consult with others.
 *
 *        Version:  1.0
 *        Created:  07/06/2012 12:41:38 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  TED (gk), guolb57@163.com
 * =====================================================================================
 */
#include <stdio.h>

#define BEGIN_CASE {
#define END_CASE }

struct rte_unpacked_struct_t
{
    char c1;
    int i;
    char c2;
    short s1;
    char c3;
};

struct rte_packed_struct_t
{
    char c1;
    int i;
    char c2;
    short s1;
    char c3;
}__attribute__((__packed__));

struct rte_unpacked_struct_t unpacked;
struct rte_packed_struct_t packed;

short *rte_get_s1(void)
{
    return &packed.s1;
}

short *rte_get_s2(void)
{
    return &unpacked.s1;
}

int main(int argc, const char *argv[])
{
#if 0
    /*case 1 begin *********************************************** */
    BEGIN_CASE
        /* passed case */
        short *val;
        val = rte_get_s1();
        printf("val = %d\n", *val);
    END_CASE
 
    BEGIN_CASE
        int *val;
        printf("%d %d\n", sizeof(unpacked), sizeof(packed));
        /* bus error here */
        val = rte_get_s2();
        printf("val = %d\n", *val);
    END_CASE
    /*case 1 end *********************************************** */
#endif

    /*case 2 begin *********************************************** */
    BEGIN_CASE
        /* passed case */
        char buf[20] = {0};
        char *p = buf;
        struct rte_unpacked_struct_t* p_unpack;

        p_unpack = (struct rte_unpacked_struct_t*)p;

        printf("%d\n", p_unpack->s1);
    END_CASE
#if 0 
    /*@todo open this case, the next case will pass, why? */
    BEGIN_CASE
        /* passed case */
        char buf[19] = {0};
        char *p = buf;
        struct rte_packed_struct_t* p_unpack;
        printf("%d %d\n", sizeof(unpacked), sizeof(packed));
        p_unpack = (struct rte_packed_struct_t*)p;
        printf("%p %p\n", p, p_unpack);
        printf("%d\n", p_unpack->s1);
    END_CASE
#endif
  
    BEGIN_CASE
        char buf[19] = {0};
        char *p = buf;
        struct rte_unpacked_struct_t* p_unpack;
        printf("%d %d\n", sizeof(unpacked), sizeof(packed));

        /* crash here [bus error] 
         * use dmesg: see "alignment trap blabla...."*/
        p_unpack = (struct rte_unpacked_struct_t*)p;
        printf("%p %p\n", p, p_unpack);
        printf("%d\n", p_unpack->s1);
    END_CASE

    /*case 2 end *********************************************** */
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值