从第一个元素到最后一个元素的 C struct 指针步骤
C struct pointer step from first to last element
提供结构测试:
#include <stdio.h>
int main() {
struct {
char* one;
char* two;
char* three;
} test;
char **ptr = &test.one;
*ptr = "one";
*++ptr = "two";
*++ptr = "three";
printf ("%s\n", test.one);
printf ("%s\n", test.two);
printf ("%s\n", test.three);
}
问题:能否保证test
结构中的元素始终处于连续的内存顺序? (所以从第一个结构元素 ++ptr
开始将始终指向 test
结构中的下一个元素?)
对于指针,您肯定会始终观察到这种行为,但 C 语言的唯一保证是元素在内存中是有序的。两者之间可能存在差距以优化字段的对齐(为了性能,特别是在 RISC 架构上)。
正确的做法是使用 offsetof 宏,或者将其设为数组。
如问题评论中所述:
Yes; the elements of a structure are stored in the order they are declared. What can upset calculations is that there may be gaps (padding) between elements. It won't happen when they're all the same type.
However, you should ask yourself: if I need to step through the elements sequentially, why am I not using an array? Arrays are designed for sequential access; structures are not (and writing code to access the elements of a structure sequentially is messy).
标准的一些相关部分是:
§6.7.2.1 Structure and union specifiers
¶6 As discussed in 6.2.5, a structure is a type consisting of a sequence of members, whose
storage is allocated in an ordered sequence, and a union is a type consisting of a sequence
of members whose storage overlap.
¶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.2.5 Types
¶20 …
- A structure type describes a sequentially allocated nonempty set of member objects
(and, in certain circumstances, an incomplete array), each of which has an optionally
specified name and possibly distinct type.
提供结构测试:
#include <stdio.h>
int main() {
struct {
char* one;
char* two;
char* three;
} test;
char **ptr = &test.one;
*ptr = "one";
*++ptr = "two";
*++ptr = "three";
printf ("%s\n", test.one);
printf ("%s\n", test.two);
printf ("%s\n", test.three);
}
问题:能否保证test
结构中的元素始终处于连续的内存顺序? (所以从第一个结构元素 ++ptr
开始将始终指向 test
结构中的下一个元素?)
对于指针,您肯定会始终观察到这种行为,但 C 语言的唯一保证是元素在内存中是有序的。两者之间可能存在差距以优化字段的对齐(为了性能,特别是在 RISC 架构上)。
正确的做法是使用 offsetof 宏,或者将其设为数组。
如问题评论中所述:
Yes; the elements of a structure are stored in the order they are declared. What can upset calculations is that there may be gaps (padding) between elements. It won't happen when they're all the same type.
However, you should ask yourself: if I need to step through the elements sequentially, why am I not using an array? Arrays are designed for sequential access; structures are not (and writing code to access the elements of a structure sequentially is messy).
标准的一些相关部分是:
§6.7.2.1 Structure and union specifiers
¶6 As discussed in 6.2.5, a structure is a type consisting of a sequence of members, whose storage is allocated in an ordered sequence, and a union is a type consisting of a sequence of members whose storage overlap.
¶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.2.5 Types
¶20 …
- A structure type describes a sequentially allocated nonempty set of member objects (and, in certain circumstances, an incomplete array), each of which has an optionally specified name and possibly distinct type.