any_t
is any type (int
, struct something
, …).
Consider this structure:
struct my_struct {
any_t val,
any_t array[10]
}
If I define a variable v
:
struct my_struct v;
Is it safe to use &v.val
as an array of 11 any_t
items?
any_t *p = &v.val;
f(p[0]);
f(p[5]);
f(p[10]);
Is it guaranteed no padding will be added between val
and array
?
From the C standard alone, it is not safe to use &v.val
as an array of 11 any_t
for these reasons:
val
and array
, because alignment requirements would not require it, but it is permitted and it is conceivably beneficial in certain circumstances, such as causing array
to be better aligned for performance (rather than necessity).val
and the array
elements were the same as an array of 11 any_t
, there is no guarantee that pointer arithmetic works. C 2011 (N1570) 6.5.6 8 defines pointer arithmetic (including array indexing), and it only requires that arithmetic work within an array (including one notional element at the end). Some C implementations use base-and-offset addressing. In such cases, a base address for val
might fail to support offsets that extend into array
.val
and therefore cannot (in accordance with the C standard) be used to address anything in array
. E.g., if you did any_t *p = &v.val; p[i] = 3; v.array[j] = 4;
, the optimizer may treat the assignments to v.array[j]
and p[i]
as independent and perform them in any order, even though you may have set i
and j
so that they would point to the same element.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With