Tips and Tricks for the ABAP® Programming Language

Y9020026 – Relative Addressing of I fields / Problems (integer)

  1. Partial association used with different relative addressing. The offset is always divisible by 4, the field length is 4 bytes constantly. The following informations are somewhat difficult to integrate for “beginners”.
  2. Relative addressing of a I-field The same logic applies analogically as with the P-fields. Here, some elementary informations once more: Basically, it can be said that I-fields must generally not be relatively addressed and be associated to a field symbol, except when using the same field length of the origin field as length and a multiple of the length as offset. This way, e.g. a field string with I-fields of equal length can be processed quite simple by a field symbol. The drawback: Uninvolved fields can e.g. be modified unintentionally by addressing errors. So, if an I-field is relatively addressed for an association to a field symbol, only other I-fields, being declared immediately after the origin field, should be addressed by this.
  3. Memory alignment of I-fields I-fields are aligned to word boundaries in memory. If e.g. a C field (e.g. with the length 1) is declared between two I-fields, padding bytes are allocated internally behind the C field. The internally created padding bytes must then be respected in relative addressing, but this is impracticable. In practice, I-fields should be declared contiguously if field symbol processing is planned.
  4. Example 1 – Here, a relative addressing was chosen, that would cause a runtime error if being activated.
  5. Example 2 – IFELD4 is addressed with “IFELD4+4(4)” and associated to the field symbol . This way, the field IFELDX, following next in the declaration, is addressed however. So, with a modification of , IFELDX is modified. The declaration of ZFELD1 would imply, that padding bytes are inserted internally. This way, the relative addressing of IFELD4 points right into other I-fields. The wrong results are processed without any error message from the SP processor. So, ZFELD1 should remain in the program just as a comment.
  6. Example 3 – IFELD4 is addressed with “IFELD4+8(4)” and associated to the field symbol . This way, the field IFELD6, following next in the declaration, is addressed however. So, with a modification of , IFELD6 is modified. Because of the fixed length of I-fields, the modification of does not produce any “trash” (like in the case of PFELD6). A prerequisite is, however, that the respective I-fields were declared contiguously.
  7. Example 4 – IFELD4 is relatively addressed by the two variables OFFX and LENX. “Unfortunately”, the offset is incorrect. The addressing points to the data field OFFX. Right, we heard that before, IFELD4 is addressed by the data field OFFX. Now, the value 111 is added to the field symbol. If the do-loop would be entered more than once, offset 523 would be “next in the queue”. The consequence, either runtime errors or further secondary errors. Summary: Caution is recommended also with relative addressing of I-fields. If addressing fields (e.g. OFFX or LENX) receive a wrong content, this causes errors. Sometimes, these errors are hard to localize and that takes time, time, time (and trouble, trouble).

ABAP-Source-Code

You can cut and paste the source code directly into the ABAP-Workbench.

REPORT Y9020026 LINE-SIZE 130.  "Release 3.1G, 4.5A
************************************************************************
* Copyright (c) 1999 by CT-Team, 33415 Verl, http://www.ct-software.com
*
*     You can use or modify this report for your own work as long
*               as you don't try to sell or republish it.
*      In no event will the author be liable for indirect, special,
*        Incidental, or consequental damages (if any) arising out of
*                       the use of this report.
*
************************************************************************
 BREAK-POINT.
*//////////////////////////////////////////////////////////////////////*
 MOVE: 'TESTREPORT for    "ASSIGN  IFeld+o(l)  TO  <fs1>"        '
        TO SY-TITLE.
*//////////////////////////////////////////////////////////////////////*
***************       Declaration of variables    **********************
 FIELD-SYMBOLS <FS1>.
*.......................................................................
 SKIP.
 DATA: IFELD4(4)   TYPE I VALUE '1022333'.   "we let the 4 stay here
*DATA: ZFELD1(2)    TYPE N.         "would all be mixed up because
*                                   "slack bytes (fill bytes) will
*                                   "be used, but no error is displayed
*                                   "Try it for yourself !
 DATA: IFELDX(4)   TYPE I VALUE '7777777'.
 DATA: IFELD6(4)   TYPE I VALUE '7333213'.
 DATA: NFELDX(400) TYPE N.
*
 DATA: OFFX    TYPE I VALUE 412.
 DATA: LENX    TYPE I VALUE 4.
*//////////////////////////////////////////////////////////////////////*
*************            -  Main Section             *******************
*//////////////////////////////////////////////////////////////////////*
*
    BREAK-POINT.
    WRITE: /5  'Example 1   **** inadmissible  ASSIGN ***' COLOR 6.
    WRITE: /10 'Inadmissible ASSIGN: ''ASSIGN IFELD4+1(3) TO <FS1>'' '.
*   ASSIGN IFELD4+1(3) TO <FS1>.
*i This command will let to a runtime error, because an I field can
*i only be accessed completely (with 4 bytes)
    ULINE.
    SKIP 2.
************************************************************************
*
 ASSIGN IFELD4+4(4) TO <FS1>.
*i What will be here assigned ?
************************************************************************
*
   BREAK-POINT.
   WRITE: / 'ASSIGN command with IFELD4, but instead IFELDX is assigned'
             COLOR 3.
    WRITE: /5 'Example 2'.
    PERFORM DISPLAY-IFELD USING IFELD4 'IFELD4'.
    WRITE: /10 'Content of   IFELDX      :', IFELDX.
    ULINE. SKIP 2.
*-----------------------------------------------------------------------
 ASSIGN IFELD4+8(4) TO <FS1>.
*******************************
*  here IFELD6 comes into play ..
*******************************
************************************************************************
*
    BREAK-POINT.
    WRITE: / 'ASSIGN mit IFELD4, but instead IFELD6 is assigned'
              COLOR 3.
    WRITE: /5 'Example 3'.
    WRITE: /10 'Content of   IFELD6      :', IFELD6.
    PERFORM DISPLAY-IFELD USING IFELD4 'IFELD4'.
    WRITE: /10 'Content of   IFELD6      :', IFELD6.
    ULINE. SKIP 2.
*-----------------------------------------------------------------------
************************************************************************
*
   BREAK-POINT.
   SKIP.
   DO 1 TIMES.
     ASSIGN IFELD4+OFFX(LENX) TO <FS1>. "<-- Zuordnung auf OFFX zu <FS1>
        WRITE: / 'ASSIGN command with IFELD4, but OFFX will be assigned'
                 COLOR 3.
        WRITE: /5 'Example 4'.
        PERFORM DISPLAY-IFELD USING IFELD4 'IFELD4'.
        ULINE. SKIP 2.
        ADD 4 TO OFFX.
   ENDDO.
*-----------------------------------------------------------------------
*-----------------------------------------------------------------------
*
*//////////////////////////////////////////////////////////////////////*
*************                Subroutines             *******************
*//////////////////////////////////////////////////////////////////////*
*
************************************************************************
*            Display of data fields and field symbols                  *
************************************************************************
 FORM DISPLAY-IFELD USING IFELD FNAME.
*
 WRITE: /10 'Content of', FNAME, ':', IFELD.
 PERFORM FELDEIGENSCHAFTEN USING IFELD.
*
 WRITE: /10 'Content of <FS1> :', <FS1>.
 PERFORM FELDEIGENSCHAFTEN USING <FS1>.
*-----------------------------------------------------------------------
 ADD   111    TO <FS1>.               "<-- The fieldsymbol will be used
*-----------------------------------------------------------------------
 WRITE: /10 'ADD 111 TO <FS1>'.
 ULINE.
*.......................................................................
 WRITE: /10 'Content of', FNAME, 35 ':', IFELD.
 WRITE: /10 'Content of <FS1>',  35 ':', <FS1>.
*.......................................................................
 ENDFORM.
************************************************************************
*    Determination of field properties (only for information)          *
************************************************************************
 FORM FELDEIGENSCHAFTEN USING ALLG.
*
 DATA: FLAENGE(2) TYPE N.
 DATA: FTYP(1) TYPE C.
 DATA: FOUT(2) TYPE N.
 DATA: FDEZ(2) TYPE N.
*.......................................................................
  ULINE.
  DESCRIBE FIELD ALLG LENGTH FLAENGE.
  WRITE: /10 'Field length  :', FLAENGE.
*
  DESCRIBE FIELD ALLG TYPE FTYP.
  WRITE: /10 'Field type    :', FTYP.
*
  DESCRIBE FIELD ALLG OUTPUT-LENGTH FOUT.
  WRITE: /10 'Output length :', FOUT.
*
  DESCRIBE FIELD ALLG DECIMALS FDEZ.
  WRITE: /10 'Decimals      :', FDEZ.
  SKIP 1.
*.......................................................................
 ENDFORM.
************************************************************************
************************************************************************
******************* END OF PROGRAM *************************************