64 位可移植性问题
所有这一切都源于我在尝试以下行时发现编译器警告消息(C4267):
const unsigned int nSize = m_vecSomeVec.size();
size()
返回一个 size_t,虽然 typedef 为 unsigned int,但实际上并不是一个 unsigned int。 我认为这与 64 位可移植性问题有关,但是有人可以为我解释一下吗? (我不只是想禁用 64 位警告。)
All this originated from me poking at a compiler warning message (C4267) when attempting the following line:
const unsigned int nSize = m_vecSomeVec.size();
size()
returns a size_t which although typedef'd to unsigned int, is not actually a unsigned int. This I believe have to do with 64 bit portability issues, however can someone explain it a bit better for me? ( I don't just want to disable 64bit warnings.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
size_t
被 typedef:ed 为unsigned int
,那么在您的特定平台上它当然是一个unsigned int
。 但它是抽象的,因此您不能总是依赖它作为无符号整数
,它在其他平台上可能会更大。可能它还没有变得更大,因为这样做的成本太高,并且例如其中包含超过 2^32 项的向量并不常见。
If
size_t
is typedef:ed tounsigned int
, then of course it is anunsigned int
, on your particular platform. But it is abstracted so that you cannot depend on it always being anunsigned int
, it might be larger on some other platform.Probably it has not been made larger since it would cost too much to do so, and e.g. vectors with more than 2^32 items in them are not very common.
根据编译器的不同,
int
在 64 位环境中可能是 32 位。Depending on the compiler,
int
may be 32-bits in 64-bit land.这取决于实施。 例如,
std::size_t
具有所需的最小大小。 但没有上限。 为了避免这种情况,请始终使用正确的 typedef:这样您将始终处于安全状态。
It depends on the implementation.
std::size_t
for example has a minimal required size. But there is no upper limit. To avoid these kind of situations, always use the proper typedef:You will be always on the safe side then.
当针对 64 位平台进行编译时,
size_t
将是 64 位类型。 因此,当启用“检测 64 位可移植性问题”时,Visual Studio 会发出有关将size_t
分配给int
的警告。Visual C++ 通过
__w64
标记获取有关size_t
的信息,例如__w64 unsigned int
。有关 64 位移植问题的更多信息,请参阅以下链接。
http://www.viva64.com/en/a/0065/
When compiling for a 64-bit platform,
size_t
will be a 64-bit type. Because of this, Visual Studio gives warnings about assigningsize_t
s toint
s when 'Detect 64-bit Portability Issues' is enabled.Visual C++ gets this information about
size_t
through the__w64
token, e.g.__w64 unsigned int
.Refer the below link for more on 64 bit porting issues..
http://www.viva64.com/en/a/0065/