FB_DynMem_Buffer
This function block provides a buffer of dynamically allocated memory. This buffer can be used for individual data or data blocks. It cannot be used for dynamic instantiation of function blocks.
During the declaration, an instance of the function block FB_DynMem_Manager is transferred to the function block, and the memory allocations are monitored as a result of this.
Declaration
fbMyBuffer : FB_DynMem_Buffer(ipMemMan := fbMyMemMan);
Properties
bAvailable: is TRUE if the buffer is available.
nBufferSize: indicates the current size of the buffer in bytes.
pBuffer: provides a pointer to the internal dynamically allocated buffer.
Methods
CreateBuffer():
The method generates the buffer by allocating memory at runtime. In addition to the desired size in bytes, a specification is issued stating whether the memory block should be zeroed.
The method returns TRUE if processing is successful.
METHOD CreateBuffer : BOOL
VAR_INPUT
nSize : UDINT; // buffer size [in bytes]
bReset : BOOL; // zero the allocated memory
END_VAR
Clear():
This method clears the buffer. The entire buffer memory is zeroed.
The method returns TRUE if processing is successful.
METHOD Clear : BOOL
VAR_INPUT
END_VAR
Resize():
This method changes the size of the buffer. You can specify whether the previous buffer content should be retained.
The method returns TRUE if processing is successful.
METHOD Resize : BOOL
VAR_INPUT
nSize : UDINT; // new buffer size [in bytes]
bPreserve : BOOL; // TRUE => preserve old content, FALSE=> don't preserve old content
bReset : BOOL; // zero the allocated memory (before preserving)
END_VAR
![]() | Changed pointer If the size of the buffer changes, the address of the buffer can also change. It is therefore absolutely necessary to obtain the new address before using the buffer. |
DeleteBuffer():
The method removes the buffer by releasing the allocated memory.
The method returns TRUE if processing is successful.
METHOD DeleteBuffer : BOOL
VAR_INPUT
END_VAR
![]() | Invalid pointer After deleting the buffer, the previously used buffer address is invalid and may no longer be used. Any pointer variables that still exist should be explicitly set to ZERO. |
Sample
The following sample code shows the use of FB_DynMem_Buffer to allocate memory during the runtime. The allocated memory is used once as a structure instance and another time as an array.
PROGRAM MAIN
VAR
bAllocateOnce : BOOL := TRUE;
fbMemMan : FB_DynMem_Manager;
fbBuffer_Struct : FB_DynMem_Buffer(ipMemMan:=fbMemMan);
pStruct : POINTER TO ST_MyStruct;
fbBuffer_Array : FB_DynMem_Buffer(ipMemMan:=fbMemMan);
pArray : POINTER TO LREAL;
nArrayLength : UINT := 10;
bResizeArray : BOOL;
END_VAR
IF bAllocateOnce THEN
bAllocateOnce := FALSE;
fbBuffer_Struct.CreateBuffer(nSize:=SIZEOF(ST_MyStruct), bReset:=TRUE);
// after successfully creating the buffer fbMemMan shows an increased nAllocatedSize [bytes] and nBufferCount=1
pStruct := fbBuffer_Struct.pBuffer;
fbBuffer_Array.CreateBuffer(nSize:=nArrayLength*SIZEOF(LREAL), bReset:=TRUE);
// after successfully creating the buffer fbMemMan shows an increased nAllocatedSize [bytes] and nBufferCount=2
pArray := fbBuffer_Array.pBuffer;
END_IF
IF pStruct <> 0 THEN
pStruct^.nCtrlValue := 7;
END_IF
IF pArray <> 0 THEN
pArray[3] := 7.5;
END_IF
IF bResizeArray THEN
bResizeArray := FALSE;
nArrayLength := 15;
fbBuffer_Array.Resize(nSize:=nArrayLength*SIZEOF(LREAL), bPreserve:=TRUE, bReset:=TRUE);
// after successfully creating the buffer fbMemMan shows an increased nAllocatedSize [bytes] and nBufferCount=2
pArray := fbBuffer_Array.pBuffer;
END_IF
Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT v3.1.4024.7 | IPC or CX (x86, x64, ARM) | Tc3_DynamicMemory |