强制转换空指针
我在较旧的 C 代码中看到过很多以下内容:
type_t *x = (type_t *) malloc(...);
既然 malloc()
返回的指针是 void *
,那么对其进行强制转换有何意义?是否因为较旧的 C 编译器不支持 void 指针,而使用 malloc()
返回 char *
来代替?
I've seen a lot of the following in older C code:
type_t *x = (type_t *) malloc(...);
What's the point of casting the pointer returned from malloc()
since it's void *
? Is it because older C compilers didn't support void pointers and malloc()
used to return char *
instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你自己的解释是正确的。 ANSI C 之前的版本('K&R' C)没有带有隐式转换的
void *
类型。char *
兼作伪void *
类型,但您需要类型强制转换的显式转换。在现代 C 语言中,这种类型转换是不受欢迎的,因为它可以抑制编译器对
malloc
原型缺失的警告。在 C++ 中,需要进行转换(但大多数时候您应该使用 new 而不是 malloc)。更新
我下面的评论试图解释为什么需要演员阵容,但有点不清楚,我将尝试在这里更好地解释它。您可能会认为,即使
malloc
返回char *
,也不需要强制转换,因为它类似于:但在本例中也需要强制转换。第二行是简单赋值运算符 (C99 6.5.1.6.1) 的约束违规。两个指针操作数都必须是兼容的类型。当您将其更改为:
约束违规消失(两个操作数现在都具有类型
char *
)并且结果定义良好(用于转换为 char 指针)。在“相反的情况”中:相同的参数适用于强制转换,但是当
int *
比char *
具有更严格的对齐要求时,结果是实现定义的< /em>。结论:在 ANSI 之前的时代,类型转换是必要的,因为
malloc
返回char *
并且不转换结果会违反“=”运算符的约束。Your own explanation is the right one. Pre-ANSI C ('K&R' C) did not have a
void *
type with implicit conversion.char *
doubled as a pseudovoid *
type, but you needed the explicit conversion of a type cast.In modern C the casting is frowned upon because it can suppress compiler warnings for a missing prototype of
malloc
. In C++, the casting is needed (but there you should be usingnew
instead ofmalloc
most of the time).Update
My comments below that try to explain why the cast is required were a bit unclear, I'll try to explain it better here. You might think that even when
malloc
returnschar *
, the cast is not needed because it is similar to:But in this example a cast is also needed. The second line is a constraint violation for the simple assignment operator (C99 6.5.1.6.1). Both pointer operands need to be of compatible type. When you change this to:
the constraint violation disappears (both operands now have type
char *
) and the result is well-defined (for converting to a char pointer). In the 'reverse situation':the same argument hold for the cast, but when
int *
has stricter alignment requirements thanchar *
, the result is implementation defined.Conclusion: In the pre-ANSI days the type cast was necessary because
malloc
returnedchar *
and not casting results is a constraint violation for the '=' operator.这里的问题是与 C 的任何方言不兼容。问题是C++。在 C++ 中,void 指针无法自动转换为任何其他指针类型。因此,如果没有显式强制转换,此代码将无法使用 C++ 编译器进行编译。
The problem here is not compatibility with any dialect of C. The problem is C++. In C++, a void pointer cannot be automatically converted to any other pointer type. So, without an explicit cast, this code would not compile with a C++ compiler.
我不知道 malloc 返回过 char* 。
但从 void* 到 type_t* (或任何其他类型)的隐式转换并不总是被允许的。
因此,需要显式转换为正确的类型。
I'm not aware that malloc ever returned a char*.
But implicit casting from void* to type_t* (or any other type) hasn't always been allowed.
Hence, the need to explicitly cast to the proper type.
恰恰相反。您需要将 void 指针转换为实际类型,然后才能使用它,因为
void *
并不表示存储在该位置的数据。Quite the contrary. You need to cast a void pointer to an actual type before you can use it, because a
void *
signifies nothing about the data stored at that location.