ARM Memory Tagging is a great example of why it's A Good Thing that pointer arithmetic and relational operators are only defined for operands within a single array.
-
-
I guess pointer arithmetic isn't okay within structures. I guess order isn't guaranteed other. I'm recalling a lot of shaky code from my memory...
-
All objects in C, including structures, have a /representation/, which is an overaid (permitted to alias) array of unsigned char [sizeof object]. Within this array relational operators and arithmetic are well-defined; otherwise offsetof would make no sense. Order is guaranteed.
-
Order is guaranteed in structures? Even on bit arrays?
-
Bitfield members don't have addresses or offsets, so it's not really meaningful to ask about their order. Of course bytes corresponding to them appear in the representation though. Within a unit the order is implementation-defined.
-
Order could be important
-
Yes. In practice implementations (ABIs) define bitfield bit order to match byte order.
-
But back to your question, for something like struct { int a, b; } c;, the < operator and other relational operators are not defined for operands &c.a and &c.b (&c.a<&c.b is UB). However (char*)&c.a < (char*)&c.b is defined in the representation array.
-
If the char* array representation is the only one doesn't one imply the other? I'm assuming there is only one "physical" block of memory defined as the structure.
- 4 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.