One specific simple example is a linked list. You could code, e.g.
DCL 1 list_entry based,
2 list_next ptr, /* -> Next entry */
2 rest_of_entry, /* Data for this list entry */
.
.
.
ALLOCATE list_entry set(new_ptr);
curr_ptr->list_next=new_ptr;
curr_ptr = new_ptr;
and you will create a new entry chained to the previous one.
It's more general than using an array.
Sounds like your programming background doesn't include languages like
C, Pascal, or assembler. I'd recommend a good book on data structures,
and the examples should translate readily to any language.
>I am a new PL/1 programmer and I don't understand the concept of pointers. Why
>are pointers actually needed? Why can't simple variable suffice? Why should I
>need the a pointer to the address of a variable? What is the purpose of BASED?
>I don't understand why I need to share storage with another variable. It seems
>to be all greek to me. Please HELP!
A pointer is just another variable type. However, it's special-- it's
value represents the location in memory to manipulate. The most
common reason for doing this is because you don't know until run-time
how much memory you will actually need or use. So, if ytou manually
use the ALLOCATE statment to allocate memory, it will hand you back a
pointer, the address of that memory, that it set aside for you. A
BASE tells the compiler how to use that memory. A variable just hides
these facts from you:
declare my_counter fixed bin(31) init(0);
declare counter_ptr pointer init(null());
declare fb31 fixed bin(31) based;
/* point to same memory location */
counter_ptr = addr( my_counter );
/* The following are the same thing */
my_counter = 123;
counter_ptr->fb31 = 123;
Pick up a good C book (like Teach Yourself C) and learn about
pointers. They are very powerful and allow you to make very good use
of dynamically controlling memory, but you can easily shoot yourself
in the foot if you make things too complex. One final note, the
difference between PL/I and most languages is that in most other
langauges the structure type and the pointer are locked together.
This isn't the case in PL/I unless the pointer is declared with the
structure. Needless to say, this is a BIG topic/concept and I can't
begin to touch it in a news group..
Good luck,
Bel, the mostly sane..
______________________________________________________________
Posted via Uncensored-News.Com, http://www.uncensored-news.com
Only $8.95 A Month, - The Worlds Uncensored News Source
now you can work with fieldxxx in the same way as if it were declared without the
based attribute. but if you have another ioarea with the same record-layout, you
can easily access these fields by simply changing the recptr to ioarea2. reference
to fieldxxxx would now adress another target. what are the benefits: it may result
in much simplier code, as you don't have to deal with 2 different named variables
(or variables with same name, but prefixed by structure-name). disadvantage: at a
time you can only access fields from one storage-area (ioarea1 or ioarea2).
but if you build a program with separatly compiled sub-routines where you process
these fields, its very easy to only pass the pointer. in the subroutine you adress
the real storage-loaction via the pointer passed. and it doesn't matter, where this
storage comes from. in my example, your subroutine operates on fieldxxx without
worrying about where it really is allocated. with passing a pointer you don't need
to pass single fields in a call.
the passing of a pointer to a structure instead of passing the structure/fields
itself is not only a good practice for passing io-areas, it can be used for any
'data'-structure you access in one procedure (for example an array).
structured arrays are another example, where based-structures could ease
programming an speed up execution:
example:
dcl 1 arr1 (500),
2 f1 char (7),
2 f2 char(8),
2 f3 dec fixed(7);
dcl idx bin fixed (15);
idx = 7;
if f3(idx) = 2 then f1(idx) = 'yes';
if f1(idx) = 'no' then f2(idx) = 'LOCAL';
else f2(idx) = 'REMOTE';
on each reference to one of the fields of the structured array, the compiler has
to calculate the target-address by taking into account the value of idx.
by using the based-approach, program writing und compiled object would be much
simplier:
dcl arr1(500) char(19);
dcl 1 str1 based(arptr),
2 f1 char (7),
2 f2 char(8),
2 f3 dec fixed(7);
dcl idx bin fixed (15);
idx = 7;
arptr = addr(arr1(idx));
if f3 = 2 then f1 = 'yes';
if f1 = 'no' then f2 = 'LOCAL';
else f2 = 'REMOTE';
but also let me append one warning:
as the based-structure doesn't allocate real storage, you must not use a
based-structure which is longer than the real storage you point to. if you do so,
you would/could adress invalid storage. to circumvent above problem in my example,
you can define the length of the arr1-elements with : dcl arr1(500)
char(cstg(str1)); --> this will always adjust the lentgth of the array-elements to
the length of your overlaying-structure str1, but will result in more object-code
and processing-time.
(and also you don't have an offset-value in your compile-listing for locating arr1
in a dump)
F.Wimmer
GRZ Genossenschaft Rechenzentrum GesmbH.
MazingerZee schrieb: