为什么我会看到“不支持未对齐的内存访问”错误?

发布于 2024-09-18 22:21:54 字数 336 浏览 2 评论 0原文

我收到“不支持未对齐内存访问错误”并进行了 Google 搜索 但没有明确的解释。 整个错误消息是:

/c:\cuda\include\math_functions_dbl_ptx1.h(1308): Error: Unaligned memory accesses not supported

以下代码导致错误:

for (j = low; j <= high; j++)

变量 j 和 high 被声明为 int。 以前遇到过这种错误,但自行解决了(我什么也没做)。

有人可以解释一下这个问题吗?

I got an "unaligned memory accesses not supported error" and did a Google search for that
but there were no clear explanations.
The whole error message is:

/c:\cuda\include\math_functions_dbl_ptx1.h(1308): Error: Unaligned memory accesses not supported

The following code caused the error:

for (j = low; j <= high; j++)

The variables j and high are declared as int.
This kind of error was encountered before but resolved by itself (I did nothing).

Can anybody explain about this matter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-09-25 22:21:54

理论

在许多机器上(但不是 Intel IA32 机器或其相关机器),如果要访问 2 字节数量(整数),则地址必须是偶数,而不是奇数;如果要访问 4 字节数量(整数或浮点数),则地址必须是 4 字节的倍数;如果要访问8字节数量(整数或双精度),则地址必须是8字节的倍数;等等。

从表面上看,您的代码以某种方式尝试取消引用一个指针,该指针在低位部分设置了位,而您不应该这样做。例如,强制解决该问题的一种方法(在 C 中)是:

long l = 0x12345678;
void *v = (char *)&l + 1;
long *lp = v;
l = *lp;

当您完成指针算术时,lp 中的地址不是 4 字节(或 8 字节)对齐;由于+1,它相差一。最后一行将给出未对齐的内存访问指针。

练习

由于您没有显示代码的声明,我们无法确定导致问题的原因(尽管您确实说 jhighint< /code> 变量;没有关于 low 的评论)。事实上,几乎独立于声明,引用的 for 循环似乎不太可能是问题的根源。它可能是接近该行的代码,但可能不是那行。

您有可能在某处遇到缓冲区覆盖问题并且意外修改了指针,并且修改后的指针会生成错误。但是,由于该行似乎不包含任何指针,因此不太可能是该行真正触发了问题。

Theory

On many machines - but not Intel IA32 ones or their relatives - if you want to access a 2-byte quantity (integer), the address must be even, not odd; if you want to access a 4-byte quantity (integer or float) the address must be a multiple of 4 bytes; if you want to access an 8-byte quantity (integer or double), the address must be a multiple of 8 bytes; and so on.

Superficially, then, you have somehow got your code trying dereference a pointer which has bits set in the low-order parts when you should not. For example, one way of forcing the issue (in C) would be:

long l = 0x12345678;
void *v = (char *)&l + 1;
long *lp = v;
l = *lp;

By the time you've gone through the pointer arithmetic, the address in lp is not 4-byte (or 8-byte) aligned; it is off-by-one because of the +1. The last line would give an unaligned memory access pointer.

Practice

Since you have not shown the declarations for your code, we can't be sure what causes the trouble (though you do say j and high are int variables; no comment about low). Indeed, almost independent of the declarations, it seems unlikely that the quoted for loop is the source of the trouble. It may be code close to that, but it probably is not that line.

There is a chance that you have a buffer over-writing problem somewhere and are modifying a pointer by accident, and that modified pointer generates the error. But, since that line does not seem to include any pointers, it is unlikely to be that line that is actually triggering the trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文