严格别名规则“的char *'指针 [英] Strict aliasing rule and 'char *' pointers

查看:89
本文介绍了严格别名规则“的char *'指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接受的答案来什么是严格别名规则?提到,您可以使用的char * 别名另一种类型而不是其他方式。

The accepted answer to What is the strict aliasing rule? mentions that you can use char * to alias another type but not the other way.

这是没有意义的,我 - 如果我们有两个指针,键入的char * 之一,另一种类型的结构的东西* 指向同一个位置,怎么可能是第一个别名第二,但第二个不别名第一?

It doesn't make sense to me — if we have two pointers, one of type char * and another of type struct something * pointing to the same location, how is it possible that the first aliases the second but the second doesn't alias the first?

推荐答案

在参考答案的措辞略有错误的,让我们得到理顺第一:
其中的目标从来没有别名另一个的目标,但两个指针可以别名同一个对象(意思是,指针指向同一个内存位置 - 马特麦克纳布指出,这仍然不是100%正确的写法,但你的想法)。此外,标准本身没有(据我所知)实际上谈论严格别名所有,但只给出了规则,可以在各种前pressions的对象可以被访问或没有。编译器标志像-fno严格走样告诉编译器是否可以假设程序员遵循这些规则(它可以根据这个假设进行优化)或不。

The wording in the referenced answer is slightly erroneous, so let's get that ironed out first: One object never aliases another object, but two pointers can "alias" the same object (meaning, the pointers point to the same memory location - as Matt McNabb pointed out, this is still not 100% correct wording but you get the Idea). Also, the standard itself doesn't (to the best of my knowledge) actually talk about strict aliasing at all, but only gives rules, through which kinds of expressions a object may be accessed or not. Compiler flags like '-fno-strict-aliasing' tell the compiler whether it can assume the programmer followed those rules (so it can perform optimizations based on that assumption) or not.

现在你的问题:所有的目标可以通过指针字符,但<$访问C $ C>字符 对象(特别是字符数组),不得通过其他大多数指针类型访问。
在此基础上编译器可以/必须做出以下假设:

Now to your question: Any object can be accessed through a pointer to char, but a char object (especially a char array) may not be accessed through most other pointer types. Based on that the compiler can/must make the following assumptions:


  1. 如果实际对象本身的类型是不知道,一个的char * T * 总是可以点同一对象(互为别名) - >对称关系

  2. 如果 T1 T2 不是相关的,而不是字符,那么 T1 * T2 * 可能永远指向同一个对象 - >对称关系

  3. A 的char * 可能指向字符 T 对象

  4. A T * 可能不指向一个字符对象的 - > 的对称关系

  1. If the type of the actual object itself is not known, a char* and T* could always point to the same object (alias each other) -> symmetric relationship.
  2. If T1and T2 are not "related" and not char, then T1* and T2* may never point to the same object -> symmetric relationship
  3. A char* may point to a char OR a T object
  4. A T* may NOT point to an char object -> asymmetric relationship

我相信,身后的不对称规则有关访问的主要理由的对象的通过指针是一个字符阵列可能不能满足如所要求的对齐一个 INT

I believe, the main rationale behind the asymmetric rules about accessing object through pointers is that a char array might not satisfy the alignment requirements of e.g. an int.

所以,即使没有基础上,严格别名规则编译器优化,例如在0x1,0x2,0x3,0x4将地址写的 INT 来一个4字节的位置字符阵列 - 在最好的情况下 - 导致性能不佳和 - 在最坏的情况 - 访问不同的内存位置,因为写一个4字节的值,当CPU的指令可能会忽略最低的两个地址位(所以在这里,这可能导致对写0x0,0x1,0x2和0x3中)。

So, even without compiler optimizations based on the strict aliasing rule, e.g. writing an int to the location of a 4-byte char array at addresses 0x1,0x2,0x3,0x4 will - in the best case - result in poor performance and - in the worst case - access a different memory location, because the CPU instructions might ignore the lowest two address bits when writing a 4-byte value (so here this might result in a write to 0x0,0x1,0x2 and 0x3).

请也意识到,相关的含义,从语言到语言(C和C ++之间)的差别,但是这是不相关的提问。

Please also be aware that the meaning of "related" differs from language to language (between C and C++), but that is not relevant for your question.

这篇关于严格别名规则“的char *'指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆