× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Main difference between C pointers and RPG pointers is that RPG pointers
don't have a data type.  RPG pointer arithmetic is done in bytes rather than
size_t quanta.  RPG pointers can't be dereferenced directly--that's where
the based variable comes in.  You can't assign the value of a dereferenced
RPG pointer to another variable.  You have to use the based variable.

To do the equivalent of a malloc, you need to define a pointer, a variable
based on it, and use the ALLOC opcode to allocate memory to it by specifying
the number of bytes.  Here's an example from TFM:

+---------------------------------------------------------------------------
-----------------------+
¦
¦
¦
¦
¦    D Ptr1            S               *
¦
¦    D Ptr2            S               *
¦
¦    C                   ALLOC     7                 Ptr1
¦
¦     * Now Ptr1 points to 7 bytes of storage
¦
¦     *
¦
¦    C                   ALLOC (E) 12345678          Ptr2
¦
¦     * This is a large amount of storage, and sometimes it may
¦
¦     * be unavailable.  If the storage could not be allocated,
¦
¦     * %ERROR will return '1', the status is set to 00426, and
¦
¦     * %STATUS will return 00426.
¦
¦
¦
¦
¦
+---------------------------------------------------------------------------
-----------------------+
Figure 193. ALLOC Operation


There are also REALLOC and DEALLOC opcodes that do what you might imagine.  

Besides the basing pointer, RPG also has the procedure pointer which can
only refer to the address of a procedure.  

The %addr() and %paddr() bifs also come in real handy.  Think of them as the
address of operator.

This is a brief introduction to a reasonably complex subject.  The best part
is that pointers offer the RPG programmer the same opportunies to generate
unpredictable results that C programmers have exploited for years.  <g>
Seriously, you can do most everything you've done in C with pointers using
pointers in RPG.  Because RPG has pass by reference, you don't need them to
the same extent you do in C.  Dynamic memory allocation is a perfect example
of a sensible use of pointers in RPG.  If you've dynamically allocated 2 d
arrays in C, you're probably prepared for the complexity of doing it in RPG.

-----Original Message-----
From: James David Rich [mailto:james@dansfoods.com]
Sent: Friday, June 23, 2000 3:39 PM
To: rpg400-l@midrange.com
Subject: pointers


Can someone explain the difference between a basing pointer in RPG and a
pointer in C?  Is there some other kind of pointer besides a basing
pointer in RPG?

Can someone explain how to do write the following C code in RPG:

void
main (){
char *buffer;

buffer = (char *) malloc (sizeof(char) * 1024);
return;
}

I know that I need to do something like:

D ptr           S       *
C                 alloc(e)      1024    ptr

but now how do I specify that ptr points to a buffer of type char?  Even
better:  how do I use alloc to allocate memory that looks like this:

D data          DS
D field                 100A

basically I want a 2-dimensional array that I can dynamically increase or
descrease.

James Rich
james@dansfoods.com

+
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.