REFERENCE 说明

1 篇文章 0 订阅
贝克霍夫信息介绍了引用(REFERENCE)在编程中的使用,它是一种更安全、更易用的指针替代品。引用无需解引用即可访问对象,且在赋值时语法更简洁,同时具备类型安全检查。引用可以通过REF=运算符分配地址,但不能对引用进行引用,也不能声明引用数组或指针。__ISVALIDREF运算符可以检查引用是否有效。在TwinCAT中,引用会被初始化为0。
摘要由CSDN通过智能技术生成

摘录:beckhoff information

说明


REFERENCE

A REFERENCE is an “alias” for an object. You can write or read an alias using identifiers. The REFERENCE data type is comparable to the POINTER data type, but REFERENCE has the following advantages over POINTER:
•Easier to use: The reference does not have to be dereferenced (with ^) to access the contents of the referenced object.
•Cleaner syntax for transferring values: If an input is a REFERENCE TO, there is no need to write ADR(value) at a(refInput := value).
•Type safety: In contrast to pointers, for references the compiler checks whether the base type is the same when assigning two references (please note that for pointers this check can be performed using the compiler extension TE1200 Static Analysis. See rule SA0019).
Syntax: < Identifier >: REFERENCE TO < data type>
Sample declaration:

refInt  : REFERENCE TO INT;
nA      : INT;
nB      : INT;

You can now use refInt as “alias” for variables of type INT.


Assignment:
The address of the reference must be set via a separate assignment operation using the allocation operator REF=. An exception to this is when an input is a REFERENCE TO and the input is transferred within the call. In this case the normal allocation operator := is used instead of the allocation operator REF=.

FUNCTION_BLOCK FB_Sample
VAR_INPUT
    refInput1  : REFERENCE TO INT; 
    refInput2  : REFERENCE TO INT;
END_VAR
PROGRAM MAIN
VAR
    fbSample   : FB_Sample;
    n1         : INT;
    n2         : INT;
END_VAR
fbSample.refInput1 REF= n1;
fbSample(refInput2 := n2);

You can check whether a reference points to a valid value (e.g. not equal to 0) using a special operator (see Checking for valid references).


Application example:

refInt REF= nA;       // refInt points now to nA
refInt := 12;         // nA has got the value 12
nB     := refInt * 2; // nB has got the value 24
refInt REF= nB;       // refInt points now to nB
refInt := nA / 2;     // nB has got the value 6
refInt REF= 0;        // explicit initialisation of the reference

References cannot be declared in the following way:(不能以以下方式声明引用:)

- REFERENCE TO REFERENCE
- ARRAY OF REFERENCE
- POINTER TO REFERENCE

Furthermore, you cannot declare a reference on a bit variable.



TwinCAT initializes references (with 0).



If a reference to an allocated input variable is used, the access (e.g. ref REF= input;) is interpreted as write access. This is not possible and leads to a compiler error during code generation.


Allocation operator REF=

The operator creates a reference (pointer) to a value.

Syntax:< Variable1> REF= < Variable2>

Beispiel:

VAR
    refA  : REFERENCE TO ST_Sample;
    stA   : ST_Sample;
                
    refB  : REFERENCE TO ST_Sample;
    stB1  : ST_Sample;
    stB2  : ST_Sample;
END_VAR
refA REF= stA;   // represents => refA := ADR(stA);

refB REF= stB1;  // represents => refB := ADR(stB1);
refA := refB;    // represents => refA^ := refB^; (value assignment of refB as refA and refB are implicitly dereferenced) 
refB := stB2;    // represents => refB^ := stB2; (value assignment of stB2 as refB is implicitly dereferenced)

Checking for valid references

You can use the operator __ISVALIDREF to check whether a reference points to a valid value, that is a value other than 0.

Syntax:

< Boolean variable> : = __ISVALIDREF (<with REFERENCE TO declared identifier);

< Boolean variable> becomes TRUE if the reference points to a valid value, otherwise FALSE.

Example:

Declaration:

nVar : INT;
refInt : REFERENCE TO INT;
refInt0: REFERENCE TO INT;
bTestref : BOOL := FALSE;
bTestref0 : BOOL := FALSE;

Implementation:

nVar := nVar + 1;
refInt REF= hugo;
refInt0 REF= 0;
bTestref := __ISVALIDREF(refInt); (* becomes TRUE, because refInt points to nVar, which is non-zero *)
bTestref0 := __ISVALIDREF(refInt0); (* becomes FALSE, because refInt is set to 0 *)

The implicit CheckPointer monitoring function affects variables of type REFERENCE in the same way as pointer variables.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值