简体   繁体   English

空指针的目的是什么?

[英]What is the purpose of void pointers?

Why are void pointers necessary, as long as one could cast any pointer type to any pointer type, ie:为什么需要 void 指针,只要可以将任何指针类型转换为任何指针类型,即:

char b = 5; 
int*a = (int*)&b;//both upcasting

or或者

int b = 10;
char*a = (char*)b;//and downcasting are allowed

? ?

Also, why there is no need for cast when using malloc/calloc/realloc?另外,为什么在使用 malloc/calloc/realloc 时不需要强制转换?

one could cast any pointer type to any pointer type可以将任何指针类型转换为任何指针类型

Not quite.不完全的。 void * is defined to convert any object pointer 1 to void * and back again with an equivalent value. void *被定义为将任何object 指针1转换为void *并以等效值再次返回。 It does not need any cast.它不需要任何演员表。

In less common architectures, the size and range of some other pointers may be smaller than void * .在不太常见的架构中,一些其他指针的大小和范围可能小于void * Casting between other pointers type may lose necessary information.在其他指针类型之间进行转换可能会丢失必要的信息。

void * provides a universal object pointer type. void *提供了一个通用的object指针类型。

void *p = any_object_pointer; // No casts required
any_object_pointer = p;       // No casts required

char * could substitute for void * , except conversion to and from other object pointers requires casts. char *可以替代void * ,除了与其他 object 指针之间的转换需要强制转换。


OP's char b = 5; int*a = (int*)&b; OP 的char b = 5; int*a = (int*)&b; char b = 5; int*a = (int*)&b; risks undefined behavior as the alignment needs of int * may exceed char * .由于 alignment 对int *的需求可能超过char * ,因此存在未定义行为的风险。


1 Function pointers may be wider than void* . 1 Function 指针可能比void*更宽。 void * and other pointers are object pointers . void *和其他指针是object 指针 C lacks a truly universal pointer type. C 缺少真正通用的指针类型。

void pointers are really useful to create a generic API. void指针对于创建通用 API 非常有用。 You might think of qsort function which can be used to sort arrays of any types.您可能会想到qsort function 可用于对任何类型的 arrays 进行排序。 void pointers can be used if the API does not know the concrete type of the pointer.如果 API 不知道指针的具体类型,则可以使用void指针。

void qsort(
   void *base,
   size_t number,
   size_t width,
   int (__cdecl *compare )(const void *, const void *)
);

Regarding allocation functions, it's the same thing.关于分配函数,也是一样的。 The C runtime does not know the type of the effective object. C 运行时不知道有效 object 的类型。 But this is not a problem as user can use generic pointer void .但这不是问题,因为用户可以使用通用指针void

So void pointers are considered as generic pointers, very useful for polymorphism, that's why the C language makes casting to void optional.所以void指针被认为是通用指针,对于多态非常有用,这就是为什么 C 语言使强制转换为void是可选的。

"Why are void pointers necessary, as long as one could cast any pointer type". “为什么需要 void 指针,只要可以转换任何指针类型”。

The alloc function would then have to use common denominator such as char.然后,分配 function 将不得不使用共同的分母,例如 char。 But then it would be confusing to have to cast to whatever we really need.但是,如果必须转换为我们真正需要的任何东西,那将是令人困惑的。 "void" just means "a bunch of bytes". “void”只是意味着“一堆字节”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM