void 指针:C 和 C 之间的区别++
我试图了解 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 typevoid *
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C 中,与
void*
的指针转换始终是隐式的。在 C++ 中,从
T*
到void*
的转换是隐式的,但void*
到其他任何内容都需要强制转换。In C, pointer conversions to and from
void*
were always implicit.In C++, conversions from
T*
tovoid*
are implicit, butvoid*
to anything else requires a cast.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.
了解指针类型转换实际上并不需要执行额外的 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.