gpt4 book ai didi

c - 仅在 C 中读取数组外部是否危险

转载 作者:行者123 更新时间:2023-11-30 20:55:23 26 4
gpt4 key购买 nike

我看过几篇关于在数组边界之外写入的危险的帖子。但我想知道,在他们之外阅读有什么问题吗?我的理由如下:

我的命令和数据位于随机生成的数组中,但有时命令需要不确定数量的数据。我是否需要在每个命令的子例程中进行检查,以便不会从字符串外部读取数据,或者我可以暂时从数组外部读取数据,然后再重新分配吗?

最佳答案

根据(草案)C 标准 (ISO 9899:201x) 附录 J,未定义行为包括:

— Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that points just beyond the array object and is used as the operand of a unary * operator that is evaluated (6.5.6).

— An array subscript is out of range, even if an object is apparently accessible with the given subscript

在 C 语言中,这个表达式 a[2] 相当于 *(a+2)

关于c - 仅在 C 中读取数组外部是否危险,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33441314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com