zoukankan      html  css  js  c++  java
  • memory alignment

    memory-alignment

    1 Conclusion

    Make sure memory alignment by 4 bytes. Especially for Embedded development.

    2 Test Program

    #include <stdio.h>
    
    // not alignment for 4B
    
    int main()
    {
        int i = 0;
        unsigned char buf[8];
    
        for (i = 0; i < 8; i++) {
            buf[i] = i;
            printf("buf[%d] = 0x%x
    ", i, buf[i]);
        }
    
        for (i = 0; i < 5; i++)
            printf("*(int *)&buf[%d] = 0x%x
    ", i, *(int *)&buf[i]);
    
        return 0;
    }
    

    3 Memory Alignment in ubuntu14.04 i386

     

    3.1 kernel version

    3.13.0-62-generic

    3.2 result

    buf[0] = 0x0
    buf[1] = 0x1
    buf[2] = 0x2
    buf[3] = 0x3
    buf[4] = 0x4
    buf[5] = 0x5
    buf[6] = 0x6
    buf[7] = 0x7
    *(int *)&buf[0] = 0x3020100
    *(int *)&buf[1] = 0x4030201
    *(int *)&buf[2] = 0x5040302
    *(int *)&buf[3] = 0x6050403
    *(int *)&buf[4] = 0x7060504
    

    4 Memory Alignment in Arm DM368

     

    4.1 kernel version

    2.6.37IPNCDM3685.1.0R001

    4.2 result

    buf[0] = 0x0
    buf[1] = 0x1
    buf[2] = 0x2
    buf[3] = 0x3
    buf[4] = 0x4
    buf[5] = 0x5
    buf[6] = 0x6
    buf[7] = 0x7
    *(int *)&buf[0] = 0x3020100
    *(int *)&buf[1] = 0x30201
    *(int *)&buf[2] = 0x1000302
    *(int *)&buf[3] = 0x2010003
    *(int *)&buf[4] = 0x7060504
    

    we can see when we reference 4B content from &buf1, &buf2 and &buf3, we get the wrong value.

  • 相关阅读:
    odbc 连接字符串
    25个国外优秀电子商务网站设计案例
    用css 添加手状样式,鼠标移上去变小手,变小手
    js如何获得FCKeditor控件的值
    导致Asp.Net站点重启的10个原因
    分享45款高质量的免费(X)HTML/CSS模板
    20110627 VisualSVN安装与配置(Delphi72010/VS2010)
    iBatis把一个表的sqlmap配置的多个xml中。
    ASP.NET State Service
    存储过程分页
  • 原文地址:https://www.cnblogs.com/aqing1987/p/4742513.html
Copyright © 2011-2022 走看看