ARTICLE
Compatibility in Structure Typing
Overview
1 Problems with final alignment gaps
2 Possible solutions when passing structures
3 Possible solutions when passing internal tables
1 Problems with final alignment gaps
Final alignment gaps can cause problems when passing structures to
formal parameters of subroutines and function modules, if these formal
parameters were typed using STRUCTURE . The
same problems arise when assigning to field
symbols that were typed using STRUCTURE . Final alignment gaps
are discussed in more detail under Data
Layout of Structures .
In non-Unicode systems, the system only checks whether or not the
actual parameters are longer than the formal parameters. In
Unicode systems ,
the system also checks whether or
not the formal parameter, including final alignment gaps, is a real
initial part of the actual parameter. This check uncovers
inconsistencies, as the following examples show:
Example 1:
Formalparameter Aktualparameter
BEGIN OF struc1, BEGIN OF struc2,
a TYPE c, a TYPE c,
b TYPE p LENGTH 5, b TYPE p LENGTH 5,
END OF struc1. c TYPE p LENGTH 3,
END OF struc2.
DATA s2 TYPE struc2.
PERFORM exp CHANGING s2.
FORM exp CHANGING s1 TYPE struc1.
CLEAR s1.
ENDFORM.
By using the CLEAR s1 statement on the formal
parameter s1, the c omponent c of the actual parameter s2
is overwritten with invalid values of the alignment gap.
Example 2:
Formalparameter Aktualparameter
BEGIN OF struc1, BEGIN OF struc2,
a TYPE i, d TYPE i,
b TYPE c, e TYPE c,
c TYPE c LENGTH 2, f TYPE p LENGTH 15,
END OF struc1. END OF struc2.
DATA s2 TYPE struc2.
PERFORM exp CHANGING s2.
FORM exp CHANGING s1 TYPE struc1.
CLEAR s1.
ENDFORM.
During passing, the component f of the structure s2 is
overwritten with the non-type-specific values of c and the final
alignment gap of the structure s1 .
s2 vorher: [ dddd | ee | ffffffffffffffff | AAA ]
s2 danach: [ aaaa | bb | ccccAAffffffffff | AAA ]
2 Possible solutions when passing structures
The best solution is to use identical types for the actual and formal
parameters. If possible, you should also use
TYPES
rather than STRUCTURE to assign types,
since typing using STRUCTURE is not allowed in
classes.
In the second example, it is absolutely necessary that you choose one
of the types. Mixing the types would have the consequence that read and
write accesses result in the processing of non-type-specific contents.
This in turn leads to undefined program behavior, which is difficult to
analyze.
A second possibility is to adapt the actual paramter type so that the
type of the formal parameter becomes a real initial part of the actual
parameter. You can do this by incorporating an
include . For the first example, it looks like
this:
Formalparameter Aktualparameter
BEGIN OF struc1, BEGIN OF struc2.
a TYPE c, INCLUDE TYPE struc1.
b TYPE p LENGTH 5, TYPES: c TYPE p LENGTH 3,
END OF struc1. END OF struc2.
This ensures that the alignment gaps of the formal parameter have the
same coverage as the alignment gaps of the actual parameter and,
therefore, the actual values cannot be overwritten.
3 Possible solutions when passing internal tables
Problems can also arise when using TABLES
parameters with STRUCTURE typing. If you pass an internal table
with a header line
to a TABLES parameter, both the header and
the table body are passed, so therefore the same inconsistencies occur
for the header as with the transfer of non-type-specific structures. In
cases, where the header is not used as a return parameter, the problem
can be avoided by passing only the table body. This is done by using
square brackets after the table name, as shown in the following
example:
DATA itab TYPE TABLE OF struc2 WITH HEADER LINE.
PERFORM exp TABLES itab[].
FORM exp TABLES t STRUCTURE struc1.
....
ENDFORM.
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved