简体   繁体   English

为什么char **(或任何T **)无效**无效?

[英]Why is char** (or any T**) to void** cast invalid?

In the first comment to Python C Module - Malloc fails in specific version of Python , @user694733 mentions that casting char** to void** is not valid. 在对Python C模块的第一条评论中--Malloc 在特定版本的Python中失败 ,@ user694733提到将char**void**无效。 I read Invalid conversion from Foo** to void** - why is implicit type conversion allowed to void* but not to void**? 我读了从Foo **到void **的无效转换 - 为什么隐式类型转换允许void *但不是void **? and http://c-faq.com/ptrs/genericpp.html but there is a reference to standard, but no real example, in which case this might be incorrect, leading to errores . http://c-faq.com/ptrs/genericpp.html但是有一个标准的引用,但没有真正的例子,在这种情况下,这可能是不正确的,导致错误。 Thinking of eg void** to double** or vice versa, is there a case where it can go wrong? 考虑例如void** double**或反之亦然,是否有可能出错的情况? Why (technically, not just because it is UB)? 为什么(从技术上讲,不仅仅因为它是UB)?

If that was allowed, it would create a loop hole in the type system: 如果允许,它将在类型系统中创建一个循环孔:

T* ptr;
void **vptr = &ptr; // &ptr is of type T**
int value;
*vptr = &value;     // &value is int*, can be converted to void*

At this point, ptr , which is according to the type system a pointer to T , is pointing to value that is an int . 此时, ptr ,根据类型系统指向T的指针,指向的valueint While the language allows you to circumvent the type system, you have to explicitly request it. 虽然该语言允许您绕过类型系统,但您必须明确请求它。 Implicit conversions are designed to avoid this type of issues. 隐式转换旨在避免此类问题。

The biggest practical problem is with multiple inheritance. 最大的实际问题是多重继承。 When you use a pointer to a class with multiple base classes, the actual value of the pointer will depend on the type of the pointer, and the compiler inserts fix-up code to adjust it when you assign from one pointer type to another. 当您使用指向具有多个基类的类的指针时,指针的实际值将取决于指针的类型,并且当您从一个指针类型分配给另一个指针类型时,编译器会插入修正代码来调整它。 When you have a pointer to the pointer, the compiler no longer has the opportunity to do those fixups, so the operation is disallowed by the standard. 当您有指向指针的指针时,编译器不再有机会执行这些修正,因此标准不允许该操作。

but there is a reference to standard, but no real example, in which case this might be incorrect, leading to errors 但是有一个标准的引用,但没有真正的例子,在这种情况下,这可能是不正确的,导致错误

This is not accurate. 这不准确。 Page http://c-faq.com/ptrs/genericpp.html which you mentioned points to another page http://c-faq.com/null/machexamp.html which contains an example of machines with different pointer sizes for different types: 页面http://c-faq.com/ptrs/genericpp.html你提到的点到另一个页面http://c-faq.com/null/machexamp.html其中包含与不同的指针尺寸不同的机器上的一个例子类型:

The Eclipse MV series from Data General has three architecturally supported pointer formats (word, byte, and bit pointers), two of which are used by C compilers: byte pointers for char * and void *, and word pointers for everything else. Data General的Eclipse MV系列有三种架构支持的指针格式(字,字和位指针),其中两个由C编译器使用:char *和void *的字节指针,以及其他所有字的指针。 For historical reasons during the evolution of the 32-bit MV line from the 16-bit Nova line, word pointers and byte pointers had the offset, indirection, and ring protection bits in different places in the word. 由于历史原因在从16位Nova线演变32位MV线期间,字指针和字节指针在字中的不同位置具有偏移,间接和环保护位。 Passing a mismatched pointer format to a function resulted in protection faults. 将不匹配的指针格式传递给函数会导致保护错误。 Eventually, the MV C compiler added many compatibility options to try to deal with code that had pointer type mismatch errors. 最终,MV C编译器添加了许多兼容性选项,以尝试处理具有指针类型不匹配错误的代码。

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

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