Pointer

Variable or function block addresses Address are saved in pointers while a program is running.

Pointer declarations have the following syntax:

<Identifier>: POINTER     TO <Datatype/Functionblock>;

A pointer can point to any data type or function block even to user-defined types.

The function of the Address Operator ADR is to assign the address of a variable or function block to the pointer.

A pointer can be dereferenced by adding the content operator “^” after the pointer identifier.

Note

A pointer is counted up byte-wise ! You can get it counted up like it is usual in the C-Compiler by using the instruction p=p+SIZEOF(p^);.

Example:

pt:POINTER TO INT;
var_int1:INT := 5;
var_int2:INT;
pt := ADR(var_int1);
var_int2:= pt^; (* var_int2 is now 5 *)

Checking pointer accesses at \ runtime

For checking pointer accesses during runtime you can create check functions, which will be called automatically before each access on the address of a pointer. For this purpose the respective function must be available in the project, directly or via a library. The following functions are supported:

  • Function CheckPointer: checks whether the address currently stored at the pointer is within the valid memory range,
  • Function CheckPointerAligned: implicates the functionality of CheckPointer and additionally checks the memory alignment.

The functions must exactly have the mentioned names. They return the address which is used for dereferencing the pointer, thus at best that which has been passed on as the first input parameter (dwAddress in the example shown below).

See in the following example of a CheckPointerAligned function, which input parameters are processed. The parameter names are examples too. A CheckPointer function must look the same, except that there may be no parameter for the granularity of the pointer access:

FUNCTION CheckPointerAligned : DWORD (* The data type of the function (return value) must be the same as used for pointers in the currently set target system; i.e. DWORD for systems using 32-bit pointers,  WORD for systems using 16-bit pointers *)
VAR_INPUT  
dwAddress     : DWORD; (* Target address of the pointer; the data type must be the same as used for pointers in the currently set target system, see above: return value *)
iSize     : DINT; (* Size of pointer access; the data type must be integer-compatible and must cover the maximum potential data size stored at the pointer address *)
iGran     : DINT; (* ! not to be used in CheckPointer functions ! : Granularity of the access, e.g. “2”, if INT is the smallest not-structured datatype used on the given address; the data type must be integer-compatible *)
bWrite:     BOOL; (*Access type: Read or Write; TRUE=read access; the data type must be BOOL *)
END_VAR  

If there are a CheckPointer function and a CheckPointerAligned function in the project, CheckPointerAligned will be called.