本文介绍了不对齐的内存访问总是导致总线错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据维基百科页面的 的,总线错误可通过对齐的内存存取所引起的。本文提供了有关如何触发总线错误的例子。在这个例子中,我们要启用对齐检查,看看总线错误。如果我们禁用此类对齐检查?

该计划似乎正常工作。我有一个程序访问未对齐的内存频繁,它使用了不少人,但没有人报告总线错误或其它奇怪的结果给我。如果我们禁用对齐检查,什么是未对齐的内存的副作用?

平台:// EN:我在 /的。我也用在Mac上GCC -arch竞价排名编译它想我的程序,它正常工作。


解决方案

  1. 这可能是显著慢访问未对齐的内存(如,几次慢)。


  2. 并非所有平台甚至支持未对齐访问 - x86和x64做,但IA64(安腾)没有,例如:


  3. 一个编译器可以的的emulate 的对齐访问(VC ++做的声明为 __对齐 ia64上的指针,例如) - 插入附加的检查,以检测不对齐的情况下,并且单独地跨在对准边界的对象的加载/存储部件。这比上本机支持它,但是平台对齐访问速度更慢。


According to the Wikipedia page Segmentation fault, a bus error can be caused by unaligned memory access. The article gives an example about how to trigger a bus error. In the example, we have to enable alignment checking to see the bus error. What if we disable such alignment checking?

The program seems to work properly. I have a program access unaligned memory frequently, and it is used by quite a few people, but no one reports bus errors or other weird results to me. If we disable alignment checking, what is the side effect of unaligned memory?

Platforms: I am working on x86/x86-64. I also tried my program by compiling it with "gcc -arch ppc" on a Mac and it works properly.

解决方案
  1. It may be significantly slower to access unaligned memory (as in, several times slower).

  2. Not all platforms even support unaligned access - x86 and x64 do, but ia64 (Itanium) does not, for example.

  3. A compiler can emulate unaligned access (VC++ does that for pointers declared as __unaligned on ia64, for example) - by inserting additional checks to detect the unaligned case, and loading/storing parts of the object that straddle the alignment boundary separately. That is even slower than unaligned access on platforms which natively support it, however.

这篇关于不对齐的内存访问总是导致总线错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:02