In the GLib documentation, there is a chapter on type conversion macros. In the discussion on converting an int
to a *void
pointer it says (emphasis mine):
在GLib文档中,有一个关于类型转换宏的章节。在讨论将int转换为空指针时,它说(强调我的):
Naively, you might try this, but it's incorrect:
很天真地,你可以尝试一下,但这是错误的:
gpointer p; int i; p = (void*) 42; i = (int) p;
Again, that example was not correct, don't copy it. The problem is that on some systems you need to do this:
同样,这个例子不正确,不要复制它。问题是在某些系统上你需要这样做:
gpointer p; int i; p = (void*) (long) 42; i = (int) (long) p;
(source: GLib Reference Manual for GLib 2.39.92, chapter Type Conversion Macros ).
(来源:GLib 2.39.92的GLib参考手册,章节类型转换宏)。
Why is that cast to long
necessary?
为什么要把它扔到很长时间?
Should any required widening of the int
not happen automatically as part of the cast to a pointer?
是否需要将int类型的扩展作为转换的一部分而自动转换为指针?
2 个解决方案
#1
5
As according to the C99: 6.3.2.3
quote:
根据C99: 6.3.2.3报价:
5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.56)
一个整数可以转换为任何指针类型。除了前面指定的,结果是实现定义的,可能不正确地对齐,可能不会指向引用类型的实体,也可能是一个陷阱代表。
6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
任何指针类型都可以转换为整数类型。除了前面指定的,结果是实现定义的。如果结果不能在整数类型中表示,则行为是未定义的。结果不需要在任何整数类型的值范围内。
According to the documentation at the link you mentioned:
根据你提到的链接文件:
Pointers are always at least 32 bits in size (on all platforms GLib intends to support). Thus you can store at least 32-bit integer values in a pointer value.
指针总是至少32位(在所有平台上,GLib都打算支持)。因此,您可以在一个指针值中存储至少32位整数值。
And further more long
is guaranteed to be atleast 32-bits.
更长的时间保证至少32位。
So,the code
所以,代码
gpointer p;
int i;
p = (void*) (long) 42;
i = (int) (long) p;
is safer,more portable and well defined for upto 32-bit integers only, as advertised by GLib.
更安全,更便携,并且仅为32位整数定义,正如GLib所宣传的那样。
#2
0
I think it is because this conversion is implementation dependendant. It is better to use uintptr_t
for this purposes, because it is exactly pointer type in particular implementation.
我认为这是因为这种转换是依赖于实现的。出于这个目的,最好使用uintptr_t,因为它是特定实现中的指针类型。
#1
5
As according to the C99: 6.3.2.3
quote:
根据C99: 6.3.2.3报价:
5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.56)
一个整数可以转换为任何指针类型。除了前面指定的,结果是实现定义的,可能不正确地对齐,可能不会指向引用类型的实体,也可能是一个陷阱代表。
6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
任何指针类型都可以转换为整数类型。除了前面指定的,结果是实现定义的。如果结果不能在整数类型中表示,则行为是未定义的。结果不需要在任何整数类型的值范围内。
According to the documentation at the link you mentioned:
根据你提到的链接文件:
Pointers are always at least 32 bits in size (on all platforms GLib intends to support). Thus you can store at least 32-bit integer values in a pointer value.
指针总是至少32位(在所有平台上,GLib都打算支持)。因此,您可以在一个指针值中存储至少32位整数值。
And further more long
is guaranteed to be atleast 32-bits.
更长的时间保证至少32位。
So,the code
所以,代码
gpointer p;
int i;
p = (void*) (long) 42;
i = (int) (long) p;
is safer,more portable and well defined for upto 32-bit integers only, as advertised by GLib.
更安全,更便携,并且仅为32位整数定义,正如GLib所宣传的那样。
#2
0
I think it is because this conversion is implementation dependendant. It is better to use uintptr_t
for this purposes, because it is exactly pointer type in particular implementation.
我认为这是因为这种转换是依赖于实现的。出于这个目的,最好使用uintptr_t,因为它是特定实现中的指针类型。