Field symbol and data reference concept in ABAP

后端 未结 4 1981
野性不改
野性不改 2021-02-06 02:03

If we compare ABAP field symbols and data references with the pointer in C, we observe :-

In C, say we declare a variable \"var\" type \"integer\" with default value \"5

4条回答
  •  野性不改
    2021-02-06 02:58

    Although data references and field symbols look very similar and are often used in a similar fashion (see the 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 a content. The only difference is that these references are pointers to other data objects, i. e. the content has a special meaning. They can point nowhere, they can be dereferenced, you can pass them along to other routines, you can manipulate either the pointer (GET REFERENCE) or the value it points to. Nothing special to it, really - just pointers as you know them from your favorite programming language.

    Field Symbols are no "real" variables. The documentation states that

    They do not physically reserve space for a field

    Field Symbols are really only clever manipulations of the local symbol table of the ABAP VM. I'll try to illustrate this - note that this is a heavily 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 others - entries that might 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 a reference variable.)

    These entries only point to an address that contains the values. Depending on the scope of these variables, they might reside in totally different memory areas, but that's not our concern at the moment. The important points are:

    • Memory has to be reserved for the variables (this is done automatically, even for references).
    • References work just like all the other variables.

    Let's add a field symbol to this:

    FIELD-SYMBOLS:  TYPE any.
    

    Then 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
        *        
    

    The field symbol is created in its initial state (unassigned). It doesn't point anywhere, and using it in this state will result in a short dump. The important point is: It is not backed by "heap" memory like the other variables. Let's

    ASSIGN my_char TO .
    

    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_CHAR
    

    Now, when accessing , the runtime system will recognize it as a field symbol, lookup the current target in the symbol table and redirect all operations to the actual location of my_char. If, on the other hand, you'd issue the command

    GET REFERENCE OF my_int INTO my_ref.
    

    the symbol table would not change, but at the "heap address" 0x123461, you'd find the "address" 0x123457. Just a value assignment like my_char = 'X' or my_int = 42 * 2.

    This is, in a very simplified version, the reason why you cannot pass field symbols as changing parameters and allow them to be reassigned inside the subroutine. They do not exist in the same way that other variables do, and they have no meaning outside of the scope of the symbol table they were added to.

提交回复
热议问题