Does the entity mean how structural elements are stored in memory

struct
{
    uint32_t i;
    uint32_t i2;
}s;
printf("%p %p", &s.i, &s.i2);

If the above example prints:

0 4

This means that the uppermost member in the structure is located at the lower memory address, and subsequent elements are stored at adjacent addresses in ascending order.

What if the platform ending is inverted? Will this picture change? Is this mentioned somewhere in the specification of any standard C?

+4
source share
5 answers

Endianness is not a factor in the decision-making process on member biases struct. The original member will always be highlighted at zero offset; the remaining members will be distributed at higher offsets in the order in which they appear in the ad struct.

- :

struct {
    uint32_t i;
    uint32_t i2;
}s;
intptr_t p = (intptr_t)&s;
intptr_t pi = (intptr_t)&s.i;
intptr_t pi2 = (intptr_t)&s.i2;
printf("%tu %tu\n", pi-p, pi2-p);

1. intptr_t , ; %tu ptrdiff_t .

:

struct S {
    uint32_t i;
    uint32_t i2;
};
printf("%tu %tu\n", offsetof(struct S, i), offsetof(struct S, i2));

2.

+5

Endianness ,

C struct ( , ), , endianness ,

, , ,

+5

Endianness .

N1570 6.7.2.1 :

  1. 6.2.5, , , , - , , .

.

+2

Endianness : integers floats. struct , . , ( ) ..

+1

, , . , endendnes - (, int32), .

0

Source: https://habr.com/ru/post/1651509/


All Articles