As you know, if a variable in C or C ++ is located at an unaligned address, then reading or writing such a variable is undefined behavior. Accordingly, if you use a packed structure, then its fields can be located at unaligned addresses and reading / writing of such fields is undefined behavior. However, at the same time, in my work, I saw a lot of C programs that, when reading from a file by functions of the _Rread ... family, put data directly into variables of the "packed structure" type, and then in the code, work with the fields of this structure is carried out as if they lie on "correct" addresses (that is, without fear of undefined behavior).
Tell me, do all these programs work as expected purely by coincidence, or does IBM i allow you to work with variables that are not aligned in memory (or rather, the processor "knows" how to work with such variables)? I myself am inclined to the second option, since, probably, the word _Packed exists for a reason, and also because such a large number of programs cannot work correctly if, in fact, the code in them can cause undefined behavior. But I have not been able to find documentary evidence of this.
Tell me, do all these programs work as expected purely by coincidence, or does IBM i allow you to work with variables that are not aligned in memory (or rather, the processor "knows" how to work with such variables)? I myself am inclined to the second option, since, probably, the word _Packed exists for a reason, and also because such a large number of programs cannot work correctly if, in fact, the code in them can cause undefined behavior. But I have not been able to find documentary evidence of this.
Comment