"__DELETE" of dynamically created struct

audi0615
2021-04-17
2021-04-18
  • audi0615 - 2021-04-17

    Hi.

    I have two sturcts, stMain and stSub.

    stSub is contained in stMain and both structs are dynamically created using __NEW.

    if I delete stMain, is stSub automatically deleted? or Should i delete stSub first and then delete stMain?

    TYPE stMain :
    STRUCT
        pstSub: POINTER TO stSub;
    END_STRUCT
    END_TYPE
    
    TYPE stSub :
    STRUCT
        xTest: BOOL;
    END_STRUCT
    END_TYPE
    
    PROGRAM PLC_PRG
    VAR
        pstMain: POINTER TO stMain;
    END_VAR
    
    IF pstMain = 0 THEN
    
        pstMain:=__NEW(stMain);
    
        IF pstMain.pstSub = 0 THEN
            pstMain.pstSub=__NEW(stSub);
        END_IF
    
    END_IF
    
    IF pstMain <> 0 THEN
        __DELETE(pstMain); //This command also deletes pstSub??
    END_IF
    
     

    Last edit: audi0615 2021-04-17
  • sgronchi - 2021-04-18

    No, it is not deleted.
    But why do you require a struct that has a pointer to struct? Couldn't you just embed stSub in stMain as a member?

     
    • audi0615 - 2021-04-18

      Thank for your reply.
      Actually the stMain has many big sized stSub members (stSub1~stSub1024) and in most cases only 1~2 of them are used depending on user configuration.
      static declaration of sub structs results in more than 500mb memory allocation for them. so this is only for reducing memory usage.

       

Log in to post a comment.