gpt4 book ai didi

c - 什么定义了内存对象 "grows"的方向

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:59 27 4
gpt4 key购买 nike

我想知道是什么定义了一个内存对象的开始地址是比对象的结束地址低还是高。例如:

char buffer[10];

char* p = &buffer[0];
printf("%p\n",p); //0x7fff064a6276

p = &buffer[9];
printf("%p\n",p); //0x7fff064a627f

在这个例子中,对象的开始地址比结束地址低。即使堆栈向低地址增长。

为什么布局与堆栈增长的方向相反?

什么定义了这个方向?语言?操作系统?编译器?处理器架构? ...

对象的结尾总是比开头的地址高吗?

最佳答案

相关标准的一部分在 §6.3.2.3 Pointers(在 §6.3 Conversions 下):

¶7 … When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.

另一个相关部分是 §6.7.2.1 结构和 union 说明符:

¶15 Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

加法(和减法)的定义部分相关(§6.5.6 加法运算符):

¶8 When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. 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; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

¶9 是定义减法行为的类似段落。

然后是 §6.5.2.1 数组下标:

¶2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

从这些,您知道转换为 char * 的对象的地址必须指向保存该对象的最低字节地址。实际上,这意味着对象的“对象指针”地址也指向最低地址。该规则绝不强制要求 int 类型的数据必须是小端或大端;两者都有效。

您还知道结构中的第一个元素在结构中的地址比后面的元素低。

关于c - 什么定义了内存对象 "grows"的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39928025/

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