强制转换空指针

发布于 2024-09-15 19:30:03 字数 244 浏览 6 评论 0原文

我在较旧的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里泪两行 2024-09-22 19:30:04

你自己的解释是正确的。 ANSI C 之前的版本('K&R' C)没有带有隐式转换的 void * 类型。 char * 兼作伪 void * 类型,但您需要类型强制转换的显式转换。

在现代 C 语言中,这种类型转换是不受欢迎的,因为它可以抑制编译器对 malloc 原型缺失的警告。在 C++ 中,需要进行转换(但大多数时候您应该使用 new 而不是 malloc)。

更新

我下面的评论试图解释为什么需要演员阵容,但有点不清楚,我将尝试在这里更好地解释它。您可能会认为,即使 malloc 返回 char *,也不需要强制转换,因为它类似于:

int  *a;
char *b = a;

但在本例中也需要强制转换。第二行是简单赋值运算符 (C99 6.5.1.6.1) 的约束违规。两个指针操作数都必须是兼容的类型。当您将其更改为:

int  *a;
char *b = (char *) a;

约束违规消失(两个操作数现在都具有类型 char *)并且结果定义良好(用于转换为 char 指针)。在“相反的情况”中:

char *c;
int  *d = (int *) c;

相同的参数适用于强制转换,但是当 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 pseudo void * 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 using new instead of malloc 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 returns char *, the cast is not needed because it is similar to:

int  *a;
char *b = a;

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:

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':

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.

Conclusion: In the pre-ANSI days the type cast was necessary because malloc returned char * and not casting results is a constraint violation for the '=' operator.

木緿 2024-09-22 19:30:04

这里的问题是与 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.

溺孤伤于心 2024-09-22 19:30:04

我不知道 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.

旧话新听 2024-09-22 19:30:04

转换从 malloc() 返回的指针有什么意义,因为它是 void *?

恰恰相反。您需要将 void 指针转换为实际类型,然后才能使用它,因为 void * 并不表示存储在该位置的数据。

What's the point of casting the pointer returned from malloc() since it's 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文