读书人

内存储器对齐的原理作用,例子以及一

发布时间: 2012-10-08 19:54:56 作者: rapoo

内存对齐的原理,作用,例子以及一些规划(中英文说明,适用sizeof结构体)

目录 ?题记?一 ?内存读取粒度 ? Memory access granularity ? 从内存的角度解释内存对齐的原理 ? 队列原理 Alignment fundamentals ? Lazy processors?二 速度 Speed (内存对齐的基本原理) ? 代码解释 ? 中文代码及其内存解释?三 不懂内存对齐将造成的可能影响如下?四 内存对齐规划 ? 内存对齐的原因 ? 对齐规则 ? 试验?五 作者
题记 下面的文章中是我对四个博客文章的合成,非原创,解释了内存对齐的原因,作用(中英文说明),及其规划!尤其适用于对sizeof结构体。首先解释了内存对齐的原理,然后对作用进行了说明,最后是例子!其中中文对内存对齐,原作者做了详细的说明及其例子解释,需要注意的是,如struct{ char?a; int?b; char?b}A;a在分配时候占用其中一个字节,剩下3个,但是b分配的是4字节,明显3个字节无法满足,那么就需要另外写入队列人觉得第二个中文作者(按我最后说明博客地址顺序)提到的最重要的是画图是一个很不错的方法.?我在引用的第四个博客中,也就是最后的博客中,通过详细的代码解释,说明了内存对齐的规划问题!?内存对齐在系统或驱动级别以至于高真实时,高保密的程序开发的时候,程序内存分配问题仍旧是保证整个程序稳定,安全,高效的基础。所以对内存对齐需要学会掌握!至少在CSDN能说的来头!

? 文章可能很杂,如果看不懂,可以直接浏览中文部分,虽然我的博客很少人来看。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? --QQ124045670

?一?? ? 内存读取粒度? ? ? ??Memory access granularity ?从内存的角度解释内存对齐的原理

Programmers are conditioned to think of memory as a simple array of bytes. Among C and its descendants,?char*?is ubiquitous as meaning "a block of memory", and even Java? has its?byte[]?type to represent raw memory.


Figure 1. How programmers see memory
内存储器对齐的原理,作用,例子以及一些规划(中英文说明,适用sizeof结构体)

However, your computer's processor does not read from and write to memory in byte-sized chunks. Instead, it accesses memory in two-, four-, eight- 16- or even 32-byte chunks. We'll call the size in which a processor accesses memory its?memory access granularity.


Figure 2. How processors see memory
内存储器对齐的原理,作用,例子以及一些规划(中英文说明,适用sizeof结构体)

The difference between how high-level programmers think of memory and how modern processors actually work with memory raises interesting issues that this article explores.

If you don't understand and address alignment issues in your software, the following scenarios, in increasing order of severity, are all possible:

读书人网 >编程

热点推荐