加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么&x 1不会引起seg故障?

发布时间:2020-12-16 10:09:05 所属栏目:百科 来源:网络整理
导读:在 this thread中,它使用 x 1来确定一些随机结构x的大小.我想知道为什么这是一个合法的解决方案?这会导致分段错误吗? 我的理解是,只要 x 1保留在当前线程可访问的内存中,它就可以了,但如果 x 1以某种方式尝试访问超出其允许范围的内存,则会导致seg错误,是
在 this thread中,它使用& x 1来确定一些随机结构x的大小.我想知道为什么这是一个合法的解决方案?这会导致分段错误吗?

我的理解是,只要& x 1保留在当前线程可访问的内存中,它就可以了,但如果& x 1以某种方式尝试访问超出其允许范围的内存,则会导致seg错误,是对的吗?

解决方法

第三,C标准明确允许指针指向一个超过数组末尾的指针.

…If both the pointer operand and the result point to elements of the
same array object,or one past the last element of the array object,
the evaluation shall not produce an overflow …

正如@alk指出的那样,在进行指针运算时,指向对象的指针被视为长度为1的数组.

For the purposes of these operators,a pointer to an object that is
not an element of an array behaves the same as a pointer to the first
element of an array of length one with the type of the object as its
element type.

(来自C99草案here的第6.5.6.8和6.5.6.7节)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读