简体   繁体   English

指向int的int指针类型

[英]Int pointer type to int

I'm having hard time working around one warning issued by VS2013. 我很难解决VS2013发出的一个警告。 What I have is a piece of code used for passing text to int var: 我所拥有的是一段用于将文本传递给int var的代码:

#define WRITE(txt)  { foo->bar.func = func_xyz; foo->bar.xyz.var = (((uint64_t) (uintptr_t) (txt)) & 0xFFFFFFFFu); } //where var is uint32_t

Which causes the compiler to return 这导致编译器返回

warning C4244: '=' : conversion from 'uintptr_t' to 'uint32_t', possible loss of data.

Now unfortunately I can't possibly change var type to anything else than uint32_t and unfortunately I fall victim to preprocessor directive 现在,很遗憾,我无法将var类型更改为uint32_t以外的任何其他内容,但不幸的是,我成为预处理器指令的受害者

#ifdef  _WIN64
    typedef unsigned __int64    uintptr_t;

Is there any way to get around this warning and do it the right way? 有什么办法可以解决这个警告,并采取正确的方法? I'm 100% sure that this is fully working (simply because it is), but any kind of warning is unacceptable. 我100%确信这是可以正常工作的(仅因为是这样),但是任何警告都是不可接受的。

One more cast should do it: 应该再进行一次转换:

#define WRITE(txt) {                                                             \
    foo->bar.func = func_xyz;                                                    \
    foo->bar.xyz.var = (uint32_t)(((uint64_t) (uintptr_t) (txt)) & 0xFFFFFFFFu); \
} //where var is uint32_t

Is there any way to get around this warning and do it the right way? 有什么办法可以解决这个警告,并采取正确的方法?

If txt can be a pointer, and you intend to capture its value in a form from which the pointer can be recovered, then no, what you are doing is inherently wrong, and the compiler is warning you about a bona fide flaw in your code. 如果txt可以是一个指针,并且您打算以一种可以从中恢复指针的形式捕获它的值,那么不,您做的是天生的错误,并且编译器警告您代码中有真正的缺陷。 。

The type uintptr_t is an unsigned integer type wide enough to hold the value of any pointer converted to integer. uintptr_t是一个无符号整数类型,其宽度足以容纳任何转换为​​整数的指针的值。 In win64, pointers can be wider than 32 bits, so uintptr_t must be wider, too. 在win64中,指针可以大于32位,因此uintptr_t必须更宽。 (And you are not a "victim" of that -- that type is specified by the standard as having those characteristics.) There is no voodoo that can make arbitrary 64-bit values fit in 32 bits. (而且您不是那种“受害者”,该类型被标准指定为具有这些特征。)没有伏都教可以使任意64位值适合32位。

I'm 100% sure that this is fully working (simply because it is), but any kind of warning is unacceptable. 我100%确信这是可以正常工作的(仅因为是这样),但是任何警告都是不可接受的。

It may have worked in all your tests, on your particular test hardware, on your particular test scenarios, and maybe even in production. 它可能已经在您的所有测试,特定的测试硬件,特定的测试方案甚至工作中都起作用。 That does not mean it is correct, or that it will work reliably on other hardware, or with other inputs. 这并不意味着它是正确的,也不意味着它可以在其他硬件或其他输入上可靠地工作。

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

相关问题 int指针类型转换失败 - int pointer type casting fails 这是一个指针还是使用arg转换为int类型? - is this a pointer or is it mulitiplying int type with arg? int *(*)(int *,int *(*)())的类型 - type of int * (*) (int * , int * (*)()) 将 int ** 分配给 int 指针 - Assigning int ** to an int pointer 编译错误'指向整数转换的不兼容指针,该指针将'int(int,int)'传递给'int'类型的参数[-Wint-conversion]“ - compiling error 'incompatible pointer to integer conversion passing 'int (int, int)' to parameter of type 'int' [-Wint-conversion]" 为什么这个警告“警告:从不兼容的指针类型‘int (*)[3]’赋值给‘int *’”? - Why this warning "warning: assignment to 'int *' from incompatible pointer type 'int (*)[3]'"? 警告:从不兼容的指针类型 const int (*)[3] 赋值给 int ** - warning: assignment to int ** from incompatible pointer type const int (*)[3] C: int 或 unsigned int 哪种类型我用于指针增加 - C: int or unsigned int which type I used for pointer increasement 不兼容的指针类型,将int [5] [5]传递给类型为int **的参数 - incompatible pointer types passing int[5][5] to parameter of type int** C-不兼容的指针类型使用类型'int的表达式初始化'int *' - C- Incompatible pointer types initializing 'int *' with an expression of type 'int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM