Stackoverflow热门问题(十)-指针的最高引用级别有多少?

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

指针的最高引用级别有多少?

  • Parag asked:
    • 对于一个变量来说,我们最多能用几级指针?
    • 例如下面这个例子:
    • int a = 10;
    • int *p = &a;
    • 同样的,有:
    • int **q = &p;
    • int ***r = &q;
    • 等等...
    • 例如:
    • int ****************zz;
  • Answers:
    • P.P - vote: 407
      • C标准规定了以下限制:
        • 5.2.4.1 Translation limits
        • 276 实现应能转换并执行至少一个包含以下限制的实例的项目:[...]
        • 279-12 指针、数组以及函数声明(在任何混合使用中)修饰一个数字、结构体、联合以及空类型声明。
      • 上面的限制是实现规定的。
        • 注:每次引用的C标准我都看不懂,各位将就一下...或者帮忙解释一下。
    • Kaz - vote: 159
      • 实际上,C程序通常间接使用无限级的指针。一二级的静态指针引用很普遍。三级的则很稀少。但无线的反而很普遍。
      • 无限级别的指针引用能在结构体的帮助下实现,而不是直接声明这种不可能的形式。以及,结构体可以实现其他数据在无限级别指针的使用。
      • struct list { struct list *next; ... };
      • 有这种用法list->next->next->next->...->next。一个多指针引用:*(*(..(*(*(*list).next).next).next...).next).next。如果.next是结构体的第一个成员,那就是一个空操作,所以它可以被当做***..***ptr
      • 链表可以不用大量表达式而轻易实现循环,并且结构体也非常容易实现环路,所以上面无限指针的使用完全没有限制。
      • 因此,链表可以作解释为解决问题增加引用等级的情况的最终例子,因为你能在每次推入操作时动态实现它。
    • Alok Save - vote: 83
      • T理论上:
        • 你能使用你想使用的任何级别的引用。
      • 实际上:
        • 因为内存不是无限的,在主机环境下会有资源限制。所以实际上实现能支持的级别有限,且需要恰当的在文档中解释。因此,虽然标准并没有限制最大值,但存在最小限制。
        • 译者注:
          • 可能是因为防止这里的环境能实现很高级别的引用,但换一个环境就不行了,
          • 因此用一个最小限制,每个环境都必须支持最小限制下的引用深度,
          • 这样至少可以保证最小限制下的引用都能正常使用。
      • 见参考:
      • C99 Standard 5.2.4.1 Translation limits:
        • -12 指针、数组以及函数声明(在任何混合使用中)修饰一个数字、结构体、联合以及空类型声明。
      • 规定了每个实现必须支持的最小限制。在脚注中,标准进一步规定:
        • 18) 实现需要尽可能避免强加转换限制。

How many levels of pointers can we have?

  • Parag asked:
    • How many pointers (*) are allowed in a single variable?
      • 对于一个变量来说,我们最多能用几级指针?
    • Let's consider the following example.
      • 例如下面这个例子:
    • int a = 10;int *p = &a;
    • Similarly we can have
      • 同样的,有:
    • int **q = &p;int ***r = &q;
    • and so on
      • 等等...
    • For example,
      • 例如:
    • int ****************zz;
  • Answers:
    • P.P - vote: 407
      • The C standard specifies the lower limit:
        • C标准规定了以下限制:
        • 5.2.4.1 Translation limits
        • 276 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits: [...]
          • 276 实现应能转换并执行至少一个包含以下限制的实例的项目:[...]
        • 279 — 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration
          • 279-12 指针、数组以及函数声明(在任何混合使用中)修饰一个数字、结构体、联合以及空类型声明。
      • The upper limit is implementation specific.
        • 上面的限制是实现规定的。
        • 注:每次引用的C标准我都看不懂,各位将就一下...或者帮忙解释一下。
    • Kaz - vote: 159
      • Actually, C programs commonly make use of infinite pointer indirection. One or two static levels are common. Triple indirection is rare. But infinite is very common.
        • 实际上,C程序通常间接使用无限级的指针。一二级的静态指针引用很普遍。三级的则很稀少。但无线的反而很普遍。
      • Infinite pointer indirection is achieved with the help of a struct, of course, not with a direct declarator, which would be impossible. And a struct is needed so that you can include other data in this structure at the different levels where this can terminate.
        • 无限级别的指针引用能在结构体的帮助下实现,而不是直接声明这种不可能的形式。以及,结构体可以实现其他数据在无限级别指针的使用。
      • struct list { struct list *next; ... };
      • now you can have list->next->next->next->...->next. This is really just multiple pointer indirections: *(*(..(*(*(*list).next).next).next...).next).next. And the .next is basically a noop when it's the first member of the structure, so we can imagine this as ***..***ptr.
        • 有这种用法list->next->next->next->...->next。一个多指针引用:*(*(..(*(*(*list).next).next).next...).next).next。如果.next是结构体的第一个成员,那就是一个空操作,所以它可以被当做***..***ptr
      • There is really no limit on this because the links can be traversed with a loop rather than a giant expression like this, and moreover, the structure can easily be made circular.
        • 链表可以不用大量表达式而轻易实现循环,并且结构体也非常容易实现环路,所以上面无限指针的使用完全没有限制。
      • Thus, in other words, linked lists may be the ultimate example of adding another level of indirection to solve a problem, since you're doing it dynamically with every push operation. 🙂
        • 因此,链表可以作解释为解决问题增加引用等级的情况的最终例子,因为你能在每次推入操作时动态实现它。
    • Alok Save - vote: 83
      • Theoretically:
        • 理论上:
      • You can have as many levels of indirections as you want.
        • 你能使用你想使用的任何级别的引用。
      • Practically:
        • 实际上:
      • Of course, nothing that consumes memory can be indefinite, there will be limitations due to resources available on the host environment. So practically there is a maximum limit to what an implementation can support and the implementation shall document it appropriately. So in all such artifacts, the standard does not specify the maximum limit, but it does specify the lower limits.
        • 因为内存不是无限的,在主机环境下会有资源限制。所以实际上实现能支持的级别有限,且需要恰当的在文档中解释。因此,虽然标准并没有限制最大值,但存在最小限制。
          • 译者注:
            • 可能是因为防止这里的环境能实现很高级别的引用,但换一个环境就不行了,
            • 因此用一个最小限制,每个环境都必须支持最小限制下的引用深度,
            • 这样至少可以保证最小限制下的引用都能正常使用。
      • Here's the reference:
        • 见参考:
      • C99 Standard 5.2.4.1 Translation limits:
        • — 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration.
          • -12 指针、数组以及函数声明(在任何混合使用中)修饰一个数字、结构体、联合以及空类型声明。
      • This specifies the lower limit that every implementation must support. Note that in a footenote the standard further says:
        • 规定了每个实现必须支持的最小限制。在脚注中,标准进一步规定:
        • 18) Implementations should avoid imposing fixed translation limits whenever possible.
          • 18) 实现需要尽可能避免强加转换限制。

版权声明:
作者:MWHLS
链接:https://mwhls.top/2510.html
来源:无镣之涯
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>