C负数组索引 [英] C negative array index

查看:109
本文介绍了C负数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的结构:

struct Node {
    struct Node* data;
    struct Node* links[4];
}

假设没有填充, Node- ; links [-1] 保证指向 Node :: data

assuming there is no padding, does Node->links[-1] guaranteed to be pointing on Node::data ?

推荐答案

不保证;这是未定义的行为:

No guarantee; this is undefined behaviour:


  • 编译器相关结构填充

  • 标准仅定义数组索引0和长度(含)

  • 可能的严格别名冲突

  • Compiler-dependent structure padding
  • Standard only defines array indexing between 0 and length (inclusive)
  • Possible strict-aliasing violation

在实践中,很有可能你最终指向数据,但任何尝试访问它将导致UB。

In practice, it's quite possible that you will end up pointing at data, but any attempts to access it will result in UB.

这篇关于C负数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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