gpt4 book ai didi

c - 什么时候指向内存区域最后一个元素的指针对指针运算有效?

转载 作者:行者123 更新时间:2023-12-05 04:23:40 25 4
gpt4 key购买 nike

C 标准说有一个指针指向“数组对象的最后一个元素”,这对于指针运算是有效的(您可以进行指针运算并获得预期结果)。

我的问题是,“array object”到底是什么意思,所以“one past”指针对算术有效?声明为某种类型的元素数组的东西是数组对象。

还有什么?

malloc() 返回的区域之后指向字符的指针,(编辑)转换为 char* ,保证对算术有效?

mmap() 返回的区域之后指向字符的指针,(编辑)转换为 char* ,保证对算术有效?

我在这些函数的手册页中找不到任何确认信息。

最佳答案

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T, the array type is sometimes called "array of T". The construction of an array type from an element type is called "array type derivation".(6.2.5, 23)

一个对象被定义为:

region of data storage in the execution environment, the contents of which can represent valuesNote 1 to entry: When referenced, an object can be interpreted as having a particular type (3.15)

void * 特别不支持算术 (UB),因此可以排除 mallocmmap。如果您为它分配或转换一些东西的数组,那么它就是公平的游戏。

@ChrisDodd 指出:

For the purposes of these operators [ed: *, +, -], 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. (6.5.6, 8)

关于c - 什么时候指向内存区域最后一个元素的指针对指针运算有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73676761/

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