简体   繁体   English

C 保证回复:指向 Void 和字符类型的指针,类型转换

[英]C Guarantees Re: Pointers to Void and Character Types, Typecasting

My best-effort reading of the C specification (C99, primarily) makes me think that it is valid to cast (or implicitly convert, where void * 's implicit conversion behavior applies), between any of these types:我对 C 规范(主要是 C99)的最大努力阅读使我认为在以下任何类型之间进行强制转换(或隐式转换,其中void *的隐式转换行为适用)是有效的:

void * , char * , signed char * , unsigned char * void * , char * , 有signed char * , unsigned char *

I expect that this will trigger no undefined behavior, and that those pointers are guaranteed to have the same underlying representation.我希望这不会触发未定义的行为,并且保证这些指针具有相同的底层表示。

Consequently, it should be possible to take a pointer of either one of those four types that is already pointing to an address which can be legally dereferenced, typecast and/or assign it to one of the three char type pointers, and dereference it to access the same memory, with the only difference being whether your code will treat the data at that location as a char , signed char , or unsigned char .因此,应该可以采用已经指向可以合法解除引用的地址的四种类型之一的指针,类型转换和/或将其分配给三个 char 类型指针之一,并解除引用它以访问相同的内存,唯一的区别是您的代码是否将该位置的数据视为charsigned charunsigned char

Is this correct?这样对吗? Is there any version of the C standard (lack of void * type in pre-standardization C not withstanding) where this is not true?是否有任何版本的 C 标准(在预标准化 C 中缺少void *类型),这不是真的?

PS I believe that this question is answered piecemeal in passing in a lot of other questions, but I've never seen a single clear answer where this is explicitly stated/confirmed. PS我相信这个问题是在传递许多其他问题时得到零碎回答的,但我从未见过明确说明/确认的明确答案。

Consequently, it should be possible to take a pointer of either one of those four types that is already pointing to an address which can be legally dereferenced, typecast and/or assign it to one of the three char type pointers, and dereference it to access the same memory, with the only difference being whether your code will treat the data at that location as a char, signed char, or unsigned char.因此,应该可以采用已经指向可以合法解除引用的地址的四种类型之一的指针,类型转换和/或将其分配给三个 char 类型指针之一,并解除引用它以访问相同的内存,唯一的区别是您的代码是否将该位置的数据视为字符、有符号字符或无符号字符。

This is correct.这是对的。 In fact you could take a valid pointer to an object of any type and convert it to some of those three and access the memory.实际上,您可以使用指向任何类型对象的有效指针并将其转换为这三个对象中的一些并访问内存。

You correctly mention the provision about void * and char * etc. having the same representation and alignment requirements, but that actually does not matter.您正确地提到了关于void *char *等具有相同表示和对齐要求的规定,但这实际上并不重要。 That refers to the properties of the pointer itself, not the properties of the objects being pointed to.那是指指针本身的属性,而不是所指向对象的属性。

The strict aliasing rule is not violated because that contains an explicit provision that a character type may be used to read or write any object.严格的别名规则没有被违反,因为它包含一个明确的规定,即可以使用字符类型读取或写入任何对象。

Note that if we have for example, signed char ch = -2;请注意,如果我们有,例如, signed char ch = -2; , or any other negative value, then (unsigned char)ch may differ from *(unsigned char *)&ch . ,或任何其他负值,则(unsigned char)ch可能与*(unsigned char *)&ch On a system with 8-bit characters, the former is guaranteed to be 254 but the latter could be 254 , 253 , or 130 depending on the numbering system in use.在具有 8 位字符的系统上,前者保证为254但后者可能为254253130具体取决于使用的编号系统。

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

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