我有一个方法
**int** create_object(Object* parent, char* name, int type, int start_block) {
...
return ptr_to_object;
}
函数内部ptr_to_object
看起来是 32 位,但一旦脱离它,它看起来就像是 64 位。
注意:我不能使用c99,我知道intptr_t
是 c99 的一部分。
您有什么建议?谢谢!
最佳答案
如果您正在处理指针,请使用指针返回类型,或者将其“隐藏”在由标准定义的足够大以支持保存指针的 int 类型中。这样的类型可以存在,但保证存在(tmk):
C99 7.20.1.4 Integer types capable of holding object pointers
The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:
intptr_t
The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:
uintptr_t
These types are optional.
其中的关键字可选。它很可能是在您选择的平台上定义的,但对于遗留可移植性,您最好返回 void *
或者声明一个类型化或空指针的专门“句柄”并返回它。
一如既往,我对使用符合 C99 的平台的任何人都非常感兴趣,这些平台选择不公开标准中作为可选功能提供的一些功能(例如这个)。如果有人有 <stdint.h>
没有 intptr_t
和/或 uintptr_t
我很好奇。如果您使用这样的工具链,请发表评论。
关于c - 指针大小、方法内的 32 位与 64 位指针大小以及方法的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13644575/