有些情况下,32位变量无法正确对齐 [英] Is there cases where a 32-bit variable could not been properly-aligned

查看:111
本文介绍了有些情况下,32位变量无法正确对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下链接中: http://msdn.microsoft.com/ en-us / library / ms684122(VS.85).aspx ,它说对正确对齐的32位变量的简单读写操作是原子操作。我想知道如果在一个c + +程序所有的32位变量是默认正确对齐。换句话说,有一个32位变量不能正确对齐的情况。

解决方案

如果你不告诉

您可以编写代码,将32位变量放置在非易失性存储器上,这样,编译器就可以正确地对齐32位变量。



< (例如通过创建 char 的数组,并将int写入数组中的奇数索引)。



您也可以使用编译器#pragmas告诉编译器来对齐特定的类型或变量。



t做任何,那么你的变量将正确对齐。


In the following link: http://msdn.microsoft.com/en-us/library/ms684122(VS.85).aspx, it is said that "Simple reads and writes to properly-aligned 32-bit variables are atomic operations". I'm wondering if in a c++ program all 32-bit variables are by default properly-aligned. Saying differently is there any case where a 32-bit variable could not been properly-aligned.

解决方案

If you don't tell the compiler to do otherwise, then it will align 32-bit variables correctly.

You can write code which places 32-bit variables at non-aligned addresses (for example by creating an array of char, and writing your int to an odd index in the array).

You can also use compiler #pragmas to tell the compiler not to align specific types or variables.

But if you don't do any of that, then your variables will be aligned properly.

这篇关于有些情况下,32位变量无法正确对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆