How to use dynamic memory in a program – Support Elsist https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/feed/ Fri, 30 Oct 2026 08:12:40 +0000 https://bbpress.org/?v=2.6.20 it-IT https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81825 How to use dynamic memory in a program https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81825 Fri, 02 May 2025 06:55:59 +0000 George There an issue about memory allocation of a function block instance and assignment to an interface. In CODESYS and TWINCAT there is the NEW operator that reserves dynamic memory to instantiate function blocks.

I tried to something similar by using sysmalloc and pointer but I am not sure that is correct way.

    IF SysFirstLoop THEN
        pFbA:= SysMAlloc(SIZEOF(fbMyA));
        pFbB:= SysMAlloc(SIZEOF(fbMyB));
    END_IF;

    CASE select    OF
        0:
        itf:= @pFbA;
        select:=2;

        1:
        itf:= @pFbB;
        select:=2;
        ELSE
    END_CASE;

    itf4.mInit(myCfg);
]]>
https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81830 Rispondi a: How to use dynamic memory in a program https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81830 Fri, 02 May 2025 07:03:56 +0000 Sergio Bertana On LogicLab programmable systems we provide two ways to allocate dynamic memory, in the allocated memory buffer it’s possible to store any kind of data but it’s not possible to instantiate FBs. The LogicLab compiler istantiates FBs only in data memory area.

SysMAlloc: This function allocate static dynamic memory and returns its address. There’s a dedicated RAM area on wich the function operates, this area is cleaned at program starting, the function reserves the defined size of memory and returns its address. The allocated area cannot be removed, it still allocated until a the program stops.

SysRMAlloc: This function allocate relocatable dynamic memory and returns its address on a pointer variable. Calling the function if the desired space of memory is available it’s cleaned and the address is saved in the defined buffer. By calling SysRMFree function the buffer is released so the memory can be reused. A proprietary garbage collector operates on the memory moving allocated buffers recovering free spaces optimizing memory use. For this reason the returned address can change during program execution and it’s necessary refer to it anytime before to use it.

So you cannot store FBs on relocatable memory, but if your own FBs use large amount of data for store variable or strings you can use SysRMalloc to create space for them on relocatable memory and freee the space after use. Many of our FBs use this technique to save memory, for example FIFOFile can be set to use dynamic memory to store the file. Since the file on the memory must be used for ever the SysRMalloc is used to allocate it.

In the ST_SendFileByEMail example program the SysRMAlloc function is used to allocate space to save the email body, when the eMail is sent the used memory can be relased.

]]>
https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81858 Rispondi a: How to use dynamic memory in a program https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81858 Fri, 02 May 2025 13:05:19 +0000 George The problem with variables of interface type that assigned with a reference of a pointer. I think it is not working as I would.

In TWINCAT it is possible that:

VAR
    pTxtLogger : POINTER TO FB_TxtLogger;
    ipLogger : ILogger;
END_VAR

pTxtLogger := __NEW(FB_TxtLogger);
ipLogger := pTxtLogger^;

IF (ipLogger <> 0) THEN
   Write := ipLogger.Write(CONCAT('logger: ', sMsg));
END_IF
]]>
https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81909 Rispondi a: How to use dynamic memory in a program https://support.elsist.biz/forums/topic/how-to-use-dynamic-memory-in-a-program/#post-81909 Mon, 05 May 2025 14:42:34 +0000 Sergio Bertana I partially correct what I said in my previous post. Even if in the the LogicLab there isn’t any NEW instruction It’s possible to bypass the obstacle by using the following approach. In the example the FB BlinkValue is instantiated on a SysMAlloc memory.

VAR
    Blk : BlinkValue;
    PBlk : ARRAY[0..3] OF @BlinkValue;
END_VAR

    // Allocates memory to instantiate FBs.

    IF (SysFirstLoop) THEN
        PBlk[0]:=SysMAlloc(SIZEOF(Blk));
        PBlk[1]:=SysMAlloc(SIZEOF(Blk));
        PBlk[2]:=SysMAlloc(SIZEOF(Blk));
    END_IF;

    // FBs executing.

    @PBlk[0].Enable:=TRUE;
    @PBlk[0].BlinkNr:=1;
    @PBlk[0]();

    @PBlk[1].Enable:=TRUE;
    @PBlk[1].BlinkNr:=2;
    @PBlk[1]();

    @PBlk[2].Enable:=TRUE;
    @PBlk[2].BlinkNr:=3;
    @PBlk[2]();

As you see to get the size of the FB it’s necessary to instantiate it on a data memory, after that the SIZEOF operator can return its size.

]]>