ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Working with unaligned variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Working with unaligned variables

    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.

  • #2
    Do the text below from the Programmer's Guide explain? Alignment should be taken care automatically. And packed structures need not be aligned. A small program to print the address of the variables may be able to demonstrate it. I will try when I have some time.

    Avoiding Field Alignment Problems in C/C++ Structures
    -----------------------------------------------------------------
    All fields defined in ILE C/C++ structures are aligned on their natural boundaries. For example, int
    fields are four bytes long and are stored on four-byte boundaries. If you create a file that is externally
    described, the system does not enforce boundary alignment of the externally described data. The
    structure may need to be packed because packed structures match the alignment of the externally
    described data.

    Comment


    • #3
      If you need to use a packed structure to read from a file, then this is not a problem. The problem is that reading fields from such a structure according to the standard is undefined behavior. But it seems to me that programs for IBM i are allowed to do this.
      Code:
      typedef _Packed struct {
        char c;
        int i;
      } Data;
      
      Data data;
      data.i = 7; // Ooops! Undefined behaviour!

      Comment


      • #4
        I know that pointers must be aligned on 16-byte boundaries. I didn't think it was important for anything else to be aligned?

        In C, in general, integers are aligned on boundaries. I have never heard that this could cause "undefined behavior", however... I thought the alignment was purely for performance reasons. Since memory is accessed 64 bits at a time, with a packed structure the CPU might have to split the integer and put half of the bits in one memory write, and the other half in a separate write. When they are aligned, it can always be done in a single write. I'm not even sure if this is needed on IBM i with its virtual architecture, et al, but it is on other platforms, and since C aims to be the same across platforms...

        But, I hadn't heard that the behavior was "undefined" for anything aside from a pointer.

        Comment


        • #5
          After looking for information on this issue, I found out that yes, the C and C ++ standards define this as undefined behavior. And this is due to the fact that some processors (I saw that they are talking about ARM) do not know how to work at unaligned addresses, causing a hardware error.
          As far as I understand, IBM i either has the ability to work with unaligned data, or compilers generate instructions so that they always work on aligned data. But in this case, as Scott noted, more instructions are required (slowing down performance) than when working with initially aligned data.
          If you take structure in RPG, then there is no such thing as alignment? How is data in an RPG structure arranged in memory? If you create a structure in RPG that consists of int and char (1) types, and then call a C program, passing this structure into it, then how can you access the fields of this structure in a C program? Should it be attached to a pointer to a packed structure or an unpacked one?

          Comment


          • #6
            RPG has keywords that will cause it to align the structures similar to how they are aligned in C.

            In RPG, it does take extra instructions to move the fields when they aren't aligned, but this is considered normal, we don't even think about it. If your system is processing at 3 billion clock cycles per second, does it matter if it requires one extra clock cycle to move data from memory to a register? Will you ever notice? When C was created in the late 1960's and early 1970's, I think this was a concern, but it isn't today.

            Comment

            Working...
            X