简体   繁体   English

C:在处理 32 位程序时,在 Windows 64 位计算机上将 int 转换为 int* 时出现警告

[英]C: Warning when casting int to int* on Windows 64-bit machine when working on 32-bit program

I'm working on a legacy 32-bit program where there are a lot of casts like DWORD* a = (DWORD*)b , where b is a native int , and I get lots of these warnings:我正在开发一个传统的 32 位程序,其中有很多类型转换,例如DWORD* a = (DWORD*)b ,其中b是本机int ,我收到很多这样的警告:

Cast to 'DWORD *' (aka 'unsigned int*') from smaller integer type 'int' ['clang: -Wint-to-pointer-cast]

Since the sizes are equal during compilation it's fine, but I don't see how Clang would know that.由于在编译过程中大小相等,这很好,但我不知道 Clang 是如何知道的。 What can I do to satisfy this warning other than disabling it entirely?除了完全禁用它之外,我还能做些什么来满足这个警告?

EDIT: The premise of the question is bad due to my misunderstanding of Clang, a compiler, and clangd, the language server which invokes Clang.编辑:由于我对编译器 Clang 和调用 Clang 的语言服务器 clangd 的误解,问题的前提很糟糕。 The language server didn't know I was targeting x86.语言服务器不知道我的目标是 x86。

So the problem is (DWORD*)b but b is of type int .所以问题是(DWORD*)bbint类型。 This means the code needs to be redesigned, because somebody is stuffing pointers into int .这意味着代码需要重新设计,因为有人将指针填充到int中。 Microsoft made a special type for a pointer-sized integer: DWORD_PTR . Microsoft 为指针大小的 integer 制作了一种特殊类型: DWORD_PTR Yeah sure there's one in stdint.h and you can use that one if you want, but if you're already using DWORD you might as well use DWORD_PTR .是的,在stdint.h中肯定有一个,如果你愿意,你可以使用那个,但如果你已经在使用DWORD你也可以使用DWORD_PTR The problem didn't happen on this line.这条线没有出现问题。 The problem happened on the line where b was assigned the value from a pointer.问题发生在从指针为b分配值的行上。

Change type of b to intptr_t , uintptr_t , or DWORD_PTR and back-propigate the change until the errors go away.b的类型更改为intptr_tuintptr_tDWORD_PTR并反向传播更改,直到错误 go 消失。 If you come to a place where you can't, that part of the code needs to be redesigned.如果你到了你不能的地方,那部分代码就需要重新设计。

Microsoft's own compiler now yields warnings for this stuff even in 32 bit compilation when the type isn't one of the pointer-in-integer types.即使在 32 位编译中,当类型不是整数指针类型之一时,Microsoft 自己的编译器现在也会对这些东西产生警告。 Best to head the warnings.最好是警告。

Stuffing pointers in integers is not a recommended practice anymore, but the Win32 API does it all over the place, so when in Rome...在整数中填充指针不再是推荐的做法,但 Win32 API 到处都是,所以在罗马时......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM