FB_BA_DynamicArray

FB_BA_DynamicArray 1:

The function block generates and deletes memory areas dynamically so that entries can be added and removed at runtime.

As soon as the maximum capacity of the array is reached, the internal memory area is automatically extended. If the capacity significantly exceeds actual demand, the internal memory area is reduced accordingly.

FB_BA_DynamicArray 2:

The internally used memory is allocated from the router memory pool and is generated via _NEW and released via _DELETE at runtime.

With each adaptation (i.e., extension or reduction in size) of the internal memory, the pointers to the obsolete/adapted memory are also invalid!

FB_BA_DynamicArray 3:

The data type of the entries is not important for the dynamic array!

The user must ensure in every case that the data type is always correctly observed by the application when dealing with contained entries.

Furthermore, all data added to the array must have a uniformly defined size!

It is recommended to use the dynamic array in particular in cases where the expected memory utilization can be estimated relatively well. Router memory is only available to a limited extent (especially with small controllers) and is to be used as efficiently as possible! If necessary, the amount of router memory available in the target system must additionally be adapted.

Syntax:

VAR_OUTPUT
  bReady    : BOOL;
  nCount    : DINT;
END_VAR

VAR
  nEntrySize     : UINT := 0;
  nMinExpCount:  : UINT := 1;
END_VAR

FB_BA_DynamicArray 4: Outputs

Name

Type

Description

bReady

BOOL

State of the allocated memory. (TRUE if at least one entry is contained in the array and memory is thus already generated).

nCount

DINT

Current number of entries contained.

Variables

Name

Type

Description

nEntrySize

UINT

Expected size of entries. Used to allocate internal memory and to manage memory areas of recorded entries.

nMinExpCount

UINT

Size by which the internal memory is to be expanded when the maximum capacity is reached (specified as the number of entries).

For further information, see examples of initialization during variable declaration.

Application

Two typical use cases are imaginable:

Case 1) Array contains data sets

In this case the array contains data sets (generic types such as BOOL, INT, STRING, or structures) by reserving internal memory in accordance with the size of the type used.

Case 2) Array contains pointers

In this case the array contains pointers to externally declared data and only memory corresponding to the size of memory addresses is reserved.

FB_BA_DynamicArray 5:

Instances of the dynamic array are not called cyclically. It is sufficient to use the management functions and properties described here.

Examples

Example 1:

Data sets of the data type ST_DATA are stored in an array.

Access to the respective data sets takes place by means of pointers to the internal memory of the array or by means of a copy of a data set.

VAR
     fbArray : FB_BA_DynamicArray := (nEntrySize:=SIZEOF(ST_Data), nMinExpCount:=5);
       stMyDataTmp  : ST_Data;
       ptrMyDataTmp : POINTER TO ST_Data;
       nIndexTmp   : DINT;
END_VAR
// 1) Save data in array and remove them with the help of index position: 
IF (fbArray.AddEntry(ADR(stMyDataTmp), nResultIndex=>nIndexTmp)) THEN 
       fbArray.RemoveEntry(nIndexTmp); 
END_IF 
 
// 2) List all data sets consecutively: 
FOR nIndexTmp = 0 TO fbArray.LastIndex DO 
    IF (fbArray.GetEntryEx(nIndexTmp, pMemoryPtr=>ptrMyDataTemp)) THEN 
       ptrMyDataTmp^.diValue := (nIndexTmp+1); 
    END_IF 
END_FOR

// 3) Get a copy of the first data set:
If (fbArray.GetEntry(0, ADR(stMyDataTmp))) THEN
    // Edit and update data set:
    stMyDataTmp.diValue := 99;

    fbArray.SetEntry(0, ADR(stMyDataTmp));
END_IF

Example 2:

The addresses of externally declared instances of the function block FB_Object are stored in an array.

VAR

    fbArray : FB_BA_DynamicArray := (uiEntrySize:=SIZEOF(POINTER TO FB_Object), uiMinExpCount:=5);
       fbMyObject1  : FB_Object;
       fbMyObject2  : FB_Object;
       fbObjectTmp  : POINTER TO FB_Object;

       nIndexTmp   : DINT;
END_VAR
// 1) Add object to array and remove it with the help of index position:
If (fbArray.AddEntryPtr(ADR(fbMyObject1), nResultIndex=>nIndexTmp)) THEN
    fbArray.RemoveEntry(nIndexTmp);
END_IF


   // 2) Add object to array and remove subsequently with the use of the pointer:
fbArray.AddEntryPtr(ADR(fbMyObject1));
fbArray.RemoveEntryExPtr(ADR(fbMyObject1));


      // 3) Determine the index position of an object within an array:
IF (fbArray.FindEntryPtr(ADR(fbMyObject1), nResultIndex=>nIndexTemp)) THEN
    // Replace entry on position "fbMyObject1" with "fbMyObject2":
    fbArray.SetEntryPtr(nIndexTmp, ADR(fbMyObject2));
ELSE
    // Error handling
    …
END_IF


   // 4) Determine first object:
IF (fbArray.GetEntry(0,ADR(fbObjTemp))) THEN
    // …
END_IF


   // 5) Remove content of the array if it has more than 10 entries:
IF(fbArray.nCount > 10) THEN
    fb_Array.Reset();
END_IF

Error messages

The following error messages may be output in the TwinCAT Output window at runtime:

Error name

Text

Description

[EDB4]

Entry-size of array not defined!

The expected size of entries was not initialized during the declaration of the array.

[EDB7]

Expansion-count of entries not defined!

The amount by which the internal memory is to be expanded was not initialized when the array was declared.

FB_BA_DynamicArray 6: Methods

Name

Definition location

Description

AddEntry

Local

Creates a new data set at the end of the array and copies the content of the specified entry to the internal memory.

FindEntry

Local

Determines the position of the specified entry in the array by comparing its content with the data sets of the array.

GetEntry

Local

Copies the contents of the data set to a certain position in the specified memory area.

GetEntryEx

Local

Determines a pointer to the internal memory of the specified data set.

RemoveEntry

Local

Removes the data set at the specified index position from the array.

RemoveEntryEx

Local

Determines the position of the specified entry and deletes it from the array.

Reset

Local

Resets the complete content of the array.

SetEntry

Local

Replaces the existing data set with a new one by overwriting the internal memory area of the existing data set with the value of the new entry.

AddEntryPtr

Local

Creates a new entry at the end of the array and copies its memory address (i.e., the address to which the pointer pEntry points) to the internal memory.

FindEntryPtr

Local

Determines the position of an entry in the array by comparing its address with the addresses stored in the array.

GetEntryExPtr

Local

Outputs a pointer to the memory address of the requested entry.

RemoveEntryExPtr

Local

Determines the position of the specified entry and deletes it from the array.

SetEntryPtr

Local

Replaces an existing entry with a new one.

FB_BA_DynamicArray 7: Properties

Name

Type

Access

Definition location

Initial value

Description

CurCapacity

DINT

Get

Local

-

Current capacity of the array (number of entries). Corresponds to the maximum number of entries that can be accepted by the internal memory.

EntrySize

UINT

Get

Local

nEntrySize

Expected size of entries that are stored in the array.

LastIndex

DINT

Get

Local

-

Index position of the last entry. This is -1 if no entries exist.

UsedMemory

UINT

Get

Local

-

Size of the internal memory consumed [bytes].