3 ms·
Heh, and they didn't even get into the aliasing rules. In embedded software, life would be a lot easier if I could hit every engineer who wants to type-pun wit
by aidenn0 13y ago
Heh, and they didn't even get into the aliasing rules. In embedded software, life would be a lot easier if I could hit every engineer who wants to type-pun without a union in the head with the ISO standard.
For this reason, a lot of compilers have options to not strictly enforce the aliasing rules
[edit]
Also C and C++ are both permitted to reorder structs, it's just that they don't because that's the easiest way to follow the standard.
- 0x09 13y ago> Also C and C++ are both permitted to reorder structs, it's just that they don't because that's the easiest way to follow the standard. Is that feasible? Doing so would require that the order of the member addresses not correspond to their layout in memory.
- pjmlp 13y agoAs long as the code is fully visible and compiled by the same compiler, then yes.
- caf 13y agoNo, in C struct members must be allocated in the order in which they are declared. C99 §6.2.5 says: 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. and §6.5.8 says: If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure,…
- mansr 13y agoWhat the language standard does not specify (and is very explicit about not doing) is padding between struct members, if any. However, the ABI spec for the compilation target details exactly how a struct must be laid out. If this were not the case, libraries could not work.
- klodolph 13y agoC is not allowed to reorder structs. There's a section in the C standard, n1516 page 113 (section 6.7.2.1 paragraph 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 which they are declared.
- jmgao 13y agoAs mentioned by others, C does not allow reordering of members in structs. However, C++ does! §9.2.12 says: Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1) This means, that under a strict reading, even the following struct can have its members reordered: struct foo { int a; public: int b; }
- ben0x539 13y agoDid this change in C++11 or does this wording mean the same thing? It's not clear to me that the implicitly public a is of a different access control than the explicitly public b according to the following wording: > Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11).
- aidenn0 13y agoThat's what I was thinking of. Thanks.
- aDevilInMe 13y ago"In embedded software, life would be a lot easier if I could hit every engineer who wants to type-pun without a union in the head with the ISO standard." Is that the same standard that says it is illegal to write to one entry and read from another in a union* ? * Note : special situation of identical fields in a structure
- mansr 13y agoThe C99 standard explicitly says type punning through a union is allowed. This is clarified in TC3 (2007) by the addition of this note in section 6.5.2.3: If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.
- aDevilInMe 13y agoThanks, I was not aware that this was corrected in a later edition.
- Too 13y agoEmbedded C developers can produce very, ehum, 'interesting' code. They usually come from electronics engineering and think in terms of registers and memory instead of an abstract machine. I've seen code that actually relies on two arrays being defined in order and thus can be indexed from only the first array but with a calculated offset. This is outside of a struct... Just type casting a struct to a char-pointer and dumping it straight out on the network or to persistent storage is code you see every day.