在 C 中使用 void* 指针的缺点

发布于 2024-07-09 04:33:04 字数 454 浏览 7 评论 0原文

在 C 中使用 void * 有很多缺点(内存相关、类型相关、效率方面......)。 尽管如此,我们还是经常使用它们,因为它们提供了灵活性。

使用 void * 列出缺点/缺点(以及 C 语言的首选解决方案 - 如果可能)。

编辑: 请访问以下链接: http:// /attractivechaos.wordpress.com/2008/10/02/using-void-in-generic-c-programming-may-be-inefficient/

There are many drawbacks to using void * in C (memory related, type related, efficiency wise ...). In spite of them we use them a lot for the flexibility they provide.

List the disadvantages/drawbacks using void * (and preferred solution in C - if possible).

EDIT:
please go through the follwoing link:
http://attractivechaos.wordpress.com/2008/10/02/using-void-in-generic-c-programming-may-be-inefficient/

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

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

发布评论

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

评论(6

难如初 2024-07-16 04:33:04

void 指针不存在效率问题。
void 指针的唯一限制是:

  • 由于明显的原因,您无法取消引用 void 指针
  • sizeof(void) 是非法的
  • 您无法对 void 指针执行指针算术

但是 GCC 假定 sizeof(void) code> 为 1,允许对 void 指针进行指针算术 - 参见这里

There are no efficiency issues with void pointers.
The only limitations with void pointers are:

  • you cannot dereference void pointer for obvious reasons
  • sizeof(void) is illegal
  • you cannot perform pointer arithmetics on void pointers

However GCC assumes that sizeof(void) is 1 and allows pointer arithmetics on void pointers - see here

快乐很简单 2024-07-16 04:33:04

我不同意问题的前提。 我们在 C 中使用 void* 是因为它是获得多态性的唯一方法。 示例:库函数 qsort 和 bsearch。 只有一个缺点,即基于 void * 的多态性是不安全的:一旦将指针强制转换为 void *,就没有什么可以阻止您通过以下方式将该 void * 强制转换为错误指针类型:错误。 我的学生经常犯这个错误。

可能会产生效率成本,因为有时需要分配堆空间才能使用多态数据结构。

任何想要了解使用 void * 的多态数据结构的优点和权衡的人都应该获得 Dave Hanson 的书 C 接口和实现

I disagree with the premise of the question. We use void* in C because it's the only way to get polymorphism. Example: library functions qsort and bsearch. There is only one drawback, which is that polymorphism based on void * is unsafe: once you cast a pointer to void *, there is nothing that prevents you from casting that void * to the wrong pointer type by mistake. My students make this mistake often.

There can be an efficiency cost because it is sometimes necessary to allocate heap space in order to use a polymorphic data structure.

Anybody who wants to see the advantages and tradeoffs in using polymorphic data structures with void * should get a copy of Dave Hanson's book C Interfaces and Implementations

舞袖。长 2024-07-16 04:33:04

呃……我不确定有那么多。 当然,除非必要,否则绝对不应该使用 void*。 如果有可以使用的明确定义的类型,那么就这样做。

根据我的经验,void* 最适合“匿名”指针(太令人震惊了!),例如 malloc() 的返回值,以及仅处理不透明的指针位缓冲区。 通常,您希望在字节级别对这些缓冲区进行寻址,然后您当然会使用unsigned char *

在需要指针的地方随意使用 void* 只会导致代码损坏、代码有异味,当然要避免。

Uh ... I'm not sure there are that many. Of course, you should never use void* when you don't have to. If there is a well-defined type you can use, then do that.

In my experience, void* is best for "anonymous" pointers (what a shock!), as in malloc()'s return value, and when just dealing with opaque buffers of bits. Oftentimes, you want to address those buffers at e.g. the byte level, and then you'd use unsigned char *, of course.

Just randomly using void* wherever you need a pointer would be just broken, smelly code, and to be avoided of course.

最单纯的乌龟 2024-07-16 04:33:04

链接到的帖子将使用 void 指针的操作与使用 C++ 模板的操作进行了比较,并得出结论:模板更有效。 这并不奇怪,而且我见过的 C++ 代码很少或从不使用 void 指针。 与其他 C++ 工具相比,它通常没有任何优势,并且可能成为类型系统中的一个漏洞。

然而,C 没有 C++ 风格的模板,并且需要 void 指针来实现必须独立于数据类型的功能。

因此,当您用 C 语言编写并且需要通用性时,空指针是获取通用性的最有效方法(因为它们是唯一的方法)。 当您使用 C++ 编写时,有更好的方法可以完成几乎任何 void 指针可以完成的任务,因此不要使用它们。

The linked-to post compares operations with void pointers to operations with C++ templates, and concludes that the templates are more efficient. This is hardly a surprise, and the C++ code I've seen uses void pointers rarely or never. It generally offers no advantage over other C++ facilities, and can be a gaping hole in the type system.

However, C does not have C++-style templates, and void pointers are necessary to implement functionality that has to be independent of data type.

Therefore, when you're writing in C, and you need genericity, void pointers are the most efficient means of getting it (since they're the only means). When you're writing in C++, there's better ways to do almost anything void pointers can accomplish, so don't use them.

岁月静好 2024-07-16 04:33:04

您给出的链接部分正确。 特别是当您不处理对象(结构)或一般情况下尚未分配的类型时。 使用 int、double 等原生类型和 void 指针(例如对于容器)几乎总是一件令人讨厌的事情,因为您可以选择将 int (对于 double 来说不起作用)转换为指针或您需要为数据类型分配额外的内存。

第一个选择很糟糕,因为它不可移植,可能不允许使用 0 作为值,而且感觉很糟糕。 第二种选择会浪费内存,并且由于额外的分配而确实会(大量)减慢速度。

但是在大多数情况下,您处理的不是本机类型,而是对象,更好地说是指向已经分配的对象的指针,至少我是这样做的。 我从来不需要整数或双精度的哈希表或映射。 仅为了指针的类型安全而使用不同的容器实现对我来说感觉是错误的,因为每个实现都会增加二进制大小。 因此,如果您只需要一个容器来存储指针,那么使用 void 指针不会减慢速度或浪费内存。

但请注意,这都是关于容器实现的,就像您提到的博客文章一样。 一般来说,如果不使用空指针,很多事情就无法完成。

Your given link, is partially true. Especially, when you are not dealing with objects (structs), or in general with types that aren't already allocated. Using native types like ints, doubles etc. and void-pointers (e.g. for a container) is nearly always an icky affair, because you have either the choice to cast the int (for a double this doesn't work) to a pointer or you need to allocate extra memory for the data type.

First choice is bad because it is not portable, 0s are maybe not allowed as a value and it simply feels bad. Second choice wastes memory and is indeed a (massive) slow down because of the extra allocations.

But in most cases you aren't dealing with native types, but with objects, better said with pointers to objects, which are already allocated, at least I do. I never needed a hash-table or a map for integers or doubles. And having different container implementations only for type safeness of pointers feels wrong to me, because every implementation will increase your binary size. So if you are only need a container to store your pointers, there is no slow down or memory waste in using void-pointers.

But note, this was all about container implementations, like btw the blog article you mentioned. In general there are many things you cannot accomplish without using void-pointers.

我不知道,我发现 void 指针对于访问不同的抽象级别(ABC)非常有效。 作为在不同抽象级别导航互连类的方法。 就是这么简单,太棒了。 就像e的公式或黄金比例一样,应该有一种崇拜虚空的神秘学*它就是那么伟大:)

I dunno, I have found void pointers to be pretty effective for accessing different levels of abstraction (ABCs). As a means of navigating interlinked classes at different levels of abstraction. Its so simple, its awesome. Like the formula for e or the golden ratio, there should be an occult that worships the void* its that great :)

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