C语言 铸造空指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3559656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Casting void pointers
提问by Blagovest Buyukliev
I've seen a lot of the following in older C code:
我在旧的 C 代码中看到了很多以下内容:
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?
投射从malloc()它返回的指针有void *什么意义?是不是因为旧的 C 编译器不支持 void 指针,而是malloc()用来返回char *?
回答by schot
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 pseudo void *type, but you needed the explicit conversion of a type cast.
你自己的解释是正确的。Pre-ANSI C ('K&R' C) 没有void *带有隐式转换的类型。char *作为伪void *类型加倍,但您需要类型转换的显式转换。
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 using newinstead of mallocmost of the time).
在现代 C 中,强制转换是不受欢迎的,因为它可以抑制缺少malloc. 在C ++中,需要铸造(但你应该使用new的,而不是malloc大部分时间)。
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 mallocreturns char *, the cast is not needed because it is similar to:
我在下面试图解释为什么需要演员表的评论有点不清楚,我会在这里尝试更好地解释它。您可能认为即使在malloc返回时char *,也不需要演员表,因为它类似于:
int *a;
char *b = a;
But in this example a cast is also needed. The second line is a constraint violationfor the simple assignment operator (C99 6.5.1.6.1). Both pointer operands need to be of compatible type. When you change this to:
但在这个例子中,还需要一个演员表。第二行违反了简单赋值运算符 (C99 6.5.1.6.1)的约束。两个指针操作数都需要是兼容的类型。当您将其更改为:
int *a;
char *b = (char *) a;
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':
约束冲突消失了(两个操作数现在都有 type char *)并且结果是明确定义的(用于转换为 char 指针)。在“相反的情况”中:
char *c;
int *d = (int *) c;
the same argument hold for the cast, but when int *has stricter alignment requirements than char *, the result is implementation defined.
相同的参数适用于强制转换,但是当int *对齐要求比 更严格时char *,结果是实现定义的。
Conclusion: In the pre-ANSI days the type cast was necessary because mallocreturned char *and not casting results is a constraint violation for the '=' operator.
结论:在 ANSI 之前的日子里,类型转换是必要的,因为malloc返回char *而不是转换结果是对 '=' 运算符的约束违规。
回答by slacker
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.
这里的问题不是与任何 C 方言兼容。问题是C++。在 C++ 中,void 指针不能自动转换为任何其他指针类型。因此,如果没有显式转换,此代码将无法使用 C++ 编译器进行编译。
回答by abelenky
I'm not aware that malloc ever returned a char*.
我不知道 malloc 曾经返回过 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* 隐式转换为 type_t*(或任何其他类型)。因此,需要显式转换为正确的类型。
回答by casablanca
What's the point of casting the pointer returned from malloc() since it's void *?
转换 malloc() 返回的指针有什么意义,因为它是 void *?
Quite the contrary. You needto 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.
恰恰相反。您需要先将 void 指针转换为实际类型,然后才能使用它,因为 a 不void *表示存储在该位置的数据。

