Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Comparing pointers not values, how ?

Former Member
0 Likes
548

Hi,

I'd like to somehow be able to compare memory locations of variables rather than their values.

For those curious why ? Well, I'd like to implement a conversion exit driven by an internal table. As the only parameters given to conversion exits are INPUT and OUTPUT I figured out I could make an internal table inside my report, store references to the variables (ie. reference to ITAB[1]-myvalue ) there and inside MATCH_FORM (or conversion exit) compare memory location of the input parameter with the locations stored inside the table of references.

Long story short(with some code snippets):

PART A.

- insert 4 numbers into an internal table it_numbers[]

- insert reference to the 4th number into an internal table it_references[]


DATA:
       it_numbers TYPE STANDARD TABLE OF i,
       it_references TYPE STANDARD TABLE OF REF TO i,
       ref_to_number TYPE REF TO i.

FIELD-SYMBOLS:
       <fs_number> LIKE LINE OF it_numbers.

APPEND 123 TO it_numbers.
APPEND 456 TO it_numbers.
APPEND 789 TO it_numbers.
APPEND 123 TO it_numbers.

READ TABLE it_numbers INDEX 4 ASSIGNING <fs_number>.
GET REFERENCE OF <fs_number> INTO ref_to_number.
APPEND ref_to_number TO it_references.

PART B.

- get the reference of passed parameter

- loop at table of references and compare whether reference to the parameter is there

- if it is there then do something


PERFORM match USING <fs_number>.

FORM match USING number TYPE i.
  DATA:
        lcl_ref_to_number TYPE REF TO i.

  FIELD-SYMBOLS:
       <fs_reference> LIKE LINE OF it_references.

  GET REFERENCE OF number INTO lcl_ref_to_number.

  LOOP AT it_references ASSIGNING <fs_reference>.
    IF <fs_reference> EQ lcl_ref_to_number.
      WRITE: / ' OK '.
    ENDIF.
  ENDLOOP.
ENDFORM.                    "match

The problem is I got lost, kept trying but was only able to achieve comparison at the value level(code above) i.e. numbers where compared and not their respective memory locations. Is is actually possible to "go the low level way" and compare memory locations rather than values ?

Cheers,

Bart

3 REPLIES 3
Read only

MarcinPciak
Active Contributor
0 Likes
505

Hi Bartosz,

Don't know actually where your problem lies, as I think your code is not comparing the values (as you think) but the data references of these values.

The ambigous here might be thinking of data objects as the physical memory descriptors. The way how they are displayed in debugger shows us that they "point" (or rather refer ) to some value behind it, but we can't see the memory address. And I am affraid that information is kept somewhere lower (at kernel level). Simple statement can prove data:



LOOP AT it_references ASSIGNING <fs_reference>.
...
 write <fs_reference>.     "this will be a syntax error
 write <fs_reference>->* . "this is fine
...
ENDLOOP.

So I as you can see we can only print the value, not the data reference. The memory address is, however, comparable. So whenever you compare two data references they must be equal as long as they refer to same data object. A prove for that:


PERFORM match USING <fs_number>.
READ TABLE it_numbers INDEX 3 ASSIGNING <fs_number>.
PERFORM match USING <fs_number>.  "for second call references don't match so you get proper message

FORM match USING number TYPE i.
  DATA:
        lcl_ref_to_number TYPE REF TO i.

  FIELD-SYMBOLS:
       <fs_reference> LIKE LINE OF it_references.

  GET REFERENCE OF number INTO lcl_ref_to_number.

  LOOP AT it_references ASSIGNING <fs_reference>.
    IF <fs_reference> EQ lcl_ref_to_number.
      WRITE: / ' OK '.
    ELSE.
      WRITE: / 'MISMATCH'.    
    ENDIF.
  ENDLOOP.
ENDFORM.                    

Regards

Marcin

Read only

danilo_henriques
Explorer
0 Likes
505

Use ( )

LOOP AT it_references ASSIGNING <fs_reference>.

IF <fs_reference> EQ (lcl_ref_to_number). <<<<<<<<

WRITE: / ' OK '.

ENDIF.

ENDLOOP.

Read only

0 Likes
505

>

> Use ( )

>

>

> LOOP AT it_references ASSIGNING <fs_reference>.

> IF <fs_reference> EQ (lcl_ref_to_number). <<<<<<<<

> WRITE: / ' OK '.

> ENDIF.

> ENDLOOP.

Did you try that yourself, or you are just guessing? Dynamic data object addressing is not allowed is such context. Besides it does not make sense.

Regards

Marcin