Field symbol and data reference in SAP-ABAP

If we match the characters of the ABAP field and the data links with a pointer to C, we observe: -

In C, let's say we declare a variable of type "var" "integer" with a default value of "5".

The variable "var" will be stored somewhere in memory and says that the memory address that contains this variable is "1000".

Now we define the pointer "ptr", and this pointer is assigned to our variable.

So, "ptr" will be "1000", and "* ptr" will be 5.

Let's compare the situation described above with ABAP.

Here we declare the field symbol "FS" and assign it to the variable "var".

Now my question is what is β€œFS”? I searched for it strictly on the Internet, but found that many ABAP consultants believe that FS has the address of a variable, that is, 1000. But this is incorrect. During debugging, I found out that fs contains only 5. Thus, fs (in ABAP) is equivalent to * ptr (in C). Please correct me if my understanding is wrong.

Now let's declare the data reference β€œdref” and another registered symbol β€œfsym”, and after creating the data reference we will assign a field symbol to it. Now we can do operations on this field symbol. So the difference between refernec data and field character: -

in the case of a field symbol, we first declare a variable and assign it a field symbol.

in the case of data access, we first lose the data binding, and then assign it to the field symbol.

Then what is the use of a data reference? The same functionality that we can achieve with a field symbol.

+6
source share
4 answers

A field symbol is very similar to a pointer, but one that you can access only in the form of a dereferenced form. In other words, it will store the memory address of the variable that was assigned to it, but will not allow you to see the memory address, but only the data that is stored in the variable that it points to. This can be proved, because if you change the contents of the field symbol pointing to the inner row of the table, you will see that the changes will be made directly in the row.

The data reference acts as a simple pointer, except that you cannot increase or decrease the memory address, as in C (ptr ++, ptr-, etc.). It is different from a field character because you can compare two data links to see if they point to the same place in memory. Comparing two field characters will be a simple comparison of values. Another difference is that you can allocate memory dynamically by creating data references using the CREATE DATA command. A field symbol can only be assigned to an already selected variable.

+12
source

Although references to data and field symbols look very similar and are often used in a similar way (see other answers), they are fundamentally different.

Data references are variables that store a value, just like a string or an integer. They have a fixed size in memory and content. The only difference is that these links are pointers to other data objects, i.e. e. content is of particular importance. They cannot point anywhere, they can be dereferenced, you can pass them to other routines, you can manipulate a pointer ( GET REFERENCE ) or the value that it points to. There is nothing special about this - just pointers, as you know, from your favorite programming language.

Field characters are not β€œreal” variables. The documentation states that

They do not physically reserve space for the field

Field symbols are really just smart manipulations with the local symbol table of the ABAP virtual machine. I will try to illustrate this - note that this is a very simplified model. Let's say you declare three variables:

 DATA: my_char TYPE c, my_int TYPE i, my_ref TYPE REF TO i. 

Then the symbol table will contain, among other things, entries that may look like this:

 name type size addr ------------------------------ MY_CHAR c 1 0x123456 MY_INT i 4 0x123457 MY_REF r ? 0x123461 

(I'm not sure about the actual size of the reference variable.)

These entries indicate only the address containing the values. Depending on the volume of these variables, they can be in completely different areas of memory, but this is not our problem at the moment. Important points:

  • Memory should be reserved for variables (this is done automatically, even for references).
  • Links work just like all other variables.

Add a field character to this:

 FIELD-SYMBOLS: <my_fs> TYPE any. 

Then the symbol may look like this:

 name type size addr target -------------------------------------- MY_CHAR c 1 0x123456 MY_INT i 4 0x123457 MY_REF r ? 0x123461 <MY_FS> * 

The field symbol is created in its initial state (not assigned). It does not indicate anywhere, and using it in this state will result in a short dump. Important point: It is not supported by heap memory, like other variables. let's

 ASSIGN my_char TO <my_fs>. 

Again the symbol might look like this:

 name type size addr target -------------------------------------- MY_CHAR c 1 0x123456 MY_INT i 4 0x123457 MY_REF r ? 0x123461 <MY_FS> * MY_CHAR 

Now, when accessing <my_fs> , the runtime system recognizes it as a field symbol, searches for the current target in the symbol table, and redirects all operations to the actual location of my_char . If, on the other hand, you have to issue a command

 GET REFERENCE OF my_int INTO my_ref. 

the character table would not change, but on the "heap address" 0x123461 you will find the "address" 0x123457. Just assigning a value, for example my_char = 'X' or my_int = 42 * 2 .

In the simplified version, why you cannot pass field characters as changing parameters and allow them to be reassigned inside a subprogram . They do not exist in the same way as other variables, and they do not matter outside the scope of the symbol table to which they were added.

+3
source

The field character, which has been in ABAP much longer, allows you to manipulate and transfer field values ​​at runtime without knowing the name of this field in advance. Consider this use case: you have a structure with 20 fields, you can refer to each field by name and assign it to a field symbol, and then change the value of a specific field, etc.

The data reference ( TYPE REF TO DATA ), which is a relatively new addition to ABAP, allows you to instantiate data at run time without knowing the type in advance using the CREATE DATA statement.

For an example of using CREATE DATA see the next SAP Help page. It shows you how you can get a reference to a reference object (for example, a reference to ABAP objects) using CREATE DATA , which you cannot do with the field symbol: http://help.sap.com/abapdocu_70/en/ABAPCREATE_DATA_REFERENCE. htm

+1
source

Field symbol - symbol for the field. You assign it to a variable and it becomes an alias for that variable.

The main difference between a link and a field symbol is that the link can point to anonymous data created by CREATE DATA or CREATE OBJECT. Field characters must always be bound to an existing variable.

0
source

Source: https://habr.com/ru/post/925286/


All Articles