Vai al contenuto

Size of data structures containing different data types

Home Forum Programmazione IEC 61131 (LogicLab) Size of data structures containing different data types

Stai visualizzando 2 post - dal 1 a 2 (di 2 totali)
  • Autore
    Post
  • #73788
    Anonimo
    Inattivo

    I am using MPS054 and I found out that the size of a defined structure containing different data types and at least one of them is long type, is related with the order of data types definition. In simulation mode, there is no problem, the size is correct. If I download the same program to the MPS054, the size is different.

    The same behaviour is for auto or mapped variables.

    myStruct: STRUCT
    lwVar: LWORD;
    diVar: DINT;
    END_STRUCT;
    
    myStruct1: STRUCT
    diVar: DINT;
    lwVar: LWORD;
    END_STRUCT;
    
    VAR
    sMyStruct : myStruct;
    sMyStruct1 : myStruct1;
    END_VAR
    
    SIZEOF(sMyStruct) => 12
    SIZEOF(sMyStruct1) => 16

    I think this problem is serious, because when I define structures I should take care of the order of data types.

    #73791
    Sergio Bertana
    Amministratore del forum

    The mismatch is a well known issue due to the data alignment in ARM systems (You can refer to a Wikipedia article). In poor words:

    • 2 bytes variables must be aligned at addresses divisible for 2
    • 4 bytes variables must be aligned at addresses divisible for 4
    • 8 bytes variables must be aligned at addresses divisible for 8

    So if you define as first structure member a 8 bytes variable and then a 4 bytes variable the length is correct. Instead of if you define as first structure member a 4 bytes variable and then a 8 bytes variable the second variable will be aligned at address divisible for 8 so 4 dummy bytes are inserted.

    The byte alignment must be taken in care also in the variable definitions, the auto variables are corectly placed by the compiler, but when you define variables on DB100 area you MUST align them correctly.

    The simulator compile the program on a X86 architecture that don’t require data alignment so the data structure size is not modified.

Stai visualizzando 2 post - dal 1 a 2 (di 2 totali)
  • Devi essere connesso per rispondere a questo topic.