void 指针:C 和 C 之间的区别++

发布于 2024-08-11 04:44:22 字数 640 浏览 2 评论 0原文

我试图了解 C 和 C++ 在 void 指针方面的差异。以下代码在 C 中编译,但在 C++ 中不编译(所有编译均使用 gcc/g++ -ansi -pedantic -Wall 完成):

int* p = malloc(sizeof(int));

因为 malloc 返回 void*,C++ 不允许这样做分配给 int* 而 C 确实允许这样做。

然而,这里:

void foo(void* vptr)
{
}

int main()
{
    int* p = (int*) malloc(sizeof(int));
    foo(p);
    return 0;
}

C++ 和 C 都毫无怨言地编译它。为什么?

K&R2 说:

任何指向对象的指针都可以 无损转换为 void * 类型 的信息。如果结果是 转换回原始指针 类型,原始指针是 恢复了。

这相当概括了 C 中有关 void* 转换的所有内容。C++ 标准规定了什么?

I'm trying to understand the differences between C and C++ with regards to void pointers. the following compiles in C but not C++ (all compilations done with gcc/g++ -ansi -pedantic -Wall):

int* p = malloc(sizeof(int));

Because malloc returns void*, which C++ doesn't allow to assign to int* while C does allow that.

However, here:

void foo(void* vptr)
{
}

int main()
{
    int* p = (int*) malloc(sizeof(int));
    foo(p);
    return 0;
}

Both C++ and C compile it with no complains. Why?

K&R2 say:

Any pointer to an object may be
converted to type void * without loss
of information. If the result is
converted back to the original pointer
type, the original pointer is
recovered.

And this pretty sums all there is about void* conversions in C. What does C++ standard dictate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

乖乖哒 2024-08-18 04:44:22

在 C 中,与 void* 的指针转换始终是隐式的。

在 C++ 中,从 T*void* 的转换是隐式的,但 void* 到其他任何内容都需要强制转换。

In C, pointer conversions to and from void* were always implicit.

In C++, conversions from T* to void* are implicit, but void* to anything else requires a cast.

垂暮老矣 2024-08-18 04:44:22

C++ 比 C 的类型更强。许多转换,特别是那些暗示对值的不同解释的转换,需要显式转换。 C++ 中的 new 运算符是一种在堆上分配内存的类型安全方法,无需显式强制转换。

C++ is more strongly-typed than C. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion. The new operator in C++ is a type-safe way to allocate memory on heap, without an explicit cast.

山人契 2024-08-18 04:44:22

了解指针类型转换实际上并不需要执行额外的 CPU 指令是很有用的。在编译时对它们进行分析,以了解开发人员的意图。 void * 是一个不透明的指针。它只说尖头物体的类型是未知的。 C 是弱类型的。它允许 (void *) 和任何 (T*) 之间隐式直接转换。 C++ 是强类型的。从 (void *) 到 (T*) 的转换对于强类型语言来说并不是很好的例子。但 C++ 必须与 C 保持向后兼容,因此它必须允许此类转换。指导原则是:显式优于隐式。因此,如果您希望将 (void*) 转换为某个特定的 (T*) 指针,则需要在代码中显式编写该指针。从 (T*) 到 (void*) 的转换不需要显式转换,因为直接对 (void*) 指针无能为力(可以不过请调用 free() )。因此 (T*) 到 (void*) 的转换非常安全。

It is useful to understand that pointer type conversions don't really require execution of extra CPU instructions. They are analysed during the compile time to understand the intensions of the developer. void * is an opaque pointer. All it says that the type of pointed object is unknown. C is weakly typed. It allows direct conversion between (void *) and any (T*) implicitly. C++ is strongly typed. A conversion from (void *) to (T*) wouldn't really make good case for a strongly typed language. But C++ had to stay backwards compatible with C, hence it had to allow such conversions. The guiding principle then is: explicit is better than implicit. Hence if you wish to convert an (void*) to some specific (T*) pointer, you need to explicitly write that in code. Conversion from (T*) to (void*) doesn't require explicit conversion since there is nothing much one can do on a (void*) pointer directly (one can call free() though). Hence (T*) to (void*) conversion is pretty much safe.

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