Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Struct alignment with FB parameters in external library

Fox
2015-06-01
2023-08-17
  • Fox - 2015-06-01

    Hi all.
    I'm developing my own external library (with Visual C++ 6.0) and lately encountered with interesting bug that I fixed in abnormal (by my consideration) way. I would like to get rid of that abnormal fixing. Currently I have no real PLC and execute my experiments with "SP PLCWinNT v2.4" simulator.
    There is an example Function Block declared in my lib-project:

    There is a h-file automatically generated by CoDeSys:

    Please notice on F_Reserv parameter. I don't need it and it is my way of fixing the following problem: when my parameter list is declared without F_Reserv - the structure alignment in the compiled C-code and in the PLC memory is different. The sizeof(LIB_FUNCtyp) is equal to 24 bytes in compiled C-code (that is perfectly correct due to 4-byte struct alignment set in project's Options) , but it seems that it equals to 21 bytes in PLC memory, cause debug process shows complete mess with values.
    So when I declare F_Reserv : ARRAY[1..3] OF BOOL in CoDeSys (forcing appropriate struct alignment) and comment (disable) the corresponding line BOOL F_RESERV[3] in C-code - my Function Block performs as designed.
    How should I get rid of that unwanted declaration? There are no any alignment parameters in Target settings of Soft PLC and it seems that it uses 1-byte alignment option.

    I feel there should be a simpler way to solve that.
    Thanks.

    IMG: Bild

    IMG: Bild

     

    Related

    Talk.ru: 3

  • shooter - 2015-06-05

    yes it does because there is a header on a structure.
    make it a little bigger and you will see more discrepancies.

     
  • Fox - 2015-06-09

    shooter hat geschrieben:
    make it a little bigger and you will see more discrepancies.
    Surely I will if struct alignment settings are diffenent
    shooter hat geschrieben:
    yes it does because there is a header on a structure.
    You mean there is no way to solve that, for example with some pragmas?
    I've tried to set 1-byte alignment option within VC++b(with "" command), but it doesn't work (I'm still trying to find out the reason).
    I'm confused

     

Log in to post a comment.