memory alignment

memory-alignment

Table of Contents

  • 1. Conclusion
  • 2. Test Program
  • 3. Memory Alignment in ubuntu14.04 i386
    • 3.1. kernel version
    • 3.2. result
  • 4. Memory Alignment in Arm DM368
    • 4.1. kernel version
    • 4.2. result

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\n", i, buf[i]);
    }

    for (i = 0; i < 5; i++)
        printf("*(int *)&buf[%d] = 0x%x\n", 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.

时间: 2024-11-08 20:58:34

memory alignment的相关文章

No Memory Alignment with GCC

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

从硬件到语言,详解C++的内存对齐(memory alignment)

转载请保留以下声明 作者:赵宗晟 出处:https://www.cnblogs.com/zhao-zongsheng/p/9099603.html 很多写C/C++的人都知道"内存对齐"的概念以及规则,但不一定对他有很深入的了解.这篇文章试着从硬件到C++语言.更彻底地讲一下C++的内存对齐. 什么是内存对齐(memory alignment) 首先,什么是内存对齐(memory alignment)?这个是从硬件层面出现的概念.大家都知道,可执行程序是由一系列CPU指令构成的.CPU

编写跨平台代码之memory alignment

编写网络包(存储在堆上)转换程序时,在hp-ux机器上运行时会遇到 si_code: 1 - BUS_ADRALN - Invalid address alignment. Please refer to the following link that helps in handling unaligned data 错误,经排查,是由于hp-ux 的cpu基于IA-64架构, 不支持内存随机存取,需要做字节序对齐操作. 当将内存数据类型进行强制转换后,随机读取的field1 地址很有可能不在一

GNU C - 关于8086的内存访问机制以及内存对齐(memory alignment)

一.为什么需要内存对齐? 无论做什么事情,我都习惯性的问自己:为什么我要去做这件事情? 是啊,这可能也是个大家都会去想的问题, 因为我们都不能稀里糊涂的或者.那为什么需要内存对齐呢?这要从cpu的内存访问机制说起. 为了了解清楚cpu的内存访问机制,昨天整晚都在查找资料,但是还是找不到很好的介绍资料.后来只是找到了相关 的一些介绍的博客. 这些博客中大多都是以介绍内存对齐为主要目的,然后顺带着说一下cpu的内存访问机制,所以 找不到权威的资料,后来听说<<汇编语言编程艺术>>这本书

C++中的new/delete与operator new/operator delete

new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数. new operator(1)调用operator new分配足够的空间,并调用相关对象的构造函数(2)不可以被重载 operator new(1)只分配所要求的空间,不调用相关对象的构造函数.当无法满足所要求分配的空间时,则        ->如果有new_handler,则调用new_handler,否则        ->如果没要求不

C++中的new、operator new与placement new

转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator delete new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数. new operator 调用operator new分配足够的空间,并调用相关对象的构造函数 不可以被重载 operator n

java命令行

Launches a Java application. Synopsis java [options] classname [args] java [options] -jar filename [args] javaw [options] classname [args] javaw [options] -jar filename [args] options Command-line options separated by spaces. See Options. classname T

YASM User Manual

This document is the user manual for the Yasm assembler. It is intended as both an introduction and a general-purpose reference for all Yasm users. 1.?Introduction Yasm is a BSD-licensed assembler that is designed from the ground up to allow for mult

BMP文件格式及读写

转 http://blog.csdn.net/pkueecser/article/details/5579604 http://blog.csdn.net/pkueecser/article/details/5573395 http://blog.csdn.net/holybin/article/details/25792741 ####################################################################################