c - 指针大小、方法内的 32 位与 64 位指针大小以及方法的返回值

标签 c pointers

我有一个方法

**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

  1. 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/

相关文章:

AT_SYMLINK_NOFOLLOW 会导致 fchmodat() 出现 EINVAL 吗?

c - 当进程的控制 shell 被杀死时优雅地终止进程

c - 如何通过指向结构的指针间接释放结构内数组中的指针并将其写入 NULL?

c++ - Pointer-to- "inner struct"成员是否被禁止?

c - 在C程序中构建docker镜像

c - 如何存储可以初始化为整数或 float 的通用四个字节

C::using sscanf 解析字符串,警告:上下文敏感:-(

c - 使用malloc()和realloc()的函数的奇怪行为导致段错误

c - 字符串中的 C 字符串指针如何​​以空值终止?

C - 访问哈希表中的结构成员时出现段错误(插入函数)