SlideShare una empresa de Scribd logo
1 de 41
Coming to Grips with Pointers and User Spaces in RPG IV Phone: +91 9971458335 e-Mail: ramanjosan@yahoo.com Ramandeep Josan
Ramandeep Josan Ramandeep Josan has worked in the development of IBM Midrange applications for 8+ years and 3 years on Java using J2ME technology. He is Project Management professional (PMBOK4 - from PMI) and Sun Java certified. He is Project manager for Satyam and has worked for various banking organizations across the globe.
Agenda ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is a pointer? Basing pointers are used to locate the storage for based variables. The storage is accessed by defining a field, array, or data structure as based on a particular basing pointer variable and setting the basing pointer variable to point to the required storage location.
What is a pointer? ,[object Object],[object Object],[object Object],Eval  Name = *Blanks  Program Storage
Pointers have always been used in . . . ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Parameter lists BProgram(I: J: K);  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> D BProgram  PI  D  X  15  D  Y  10  D  Z  5  Storage of Calling Program I J K Storage of Called Program X Y Z
The problem with pointers BProgram(I: J: K); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  D BProgram  PI  D  X  15  D  Y  10  D  Z  15  Z = *Blanks;  Storage of Calling Program I J K Storage of Called Program X Y Z Oops!
The problem with pointers BProgram(I: J: K); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  D BProgram  PI  D  X  15  D  Y  10  D  Z  15  Z = *Blanks;  Storage of Calling Program I J K Storage of Called Program X Y Z Oops!
Better get used to . . . Message ID . . . . . . :  RNQ0222  Date sent  . . . . . . :  13/09/02  Time sent  . . . . . . :  11:40:31  Message . . . . :  Pointer or parameter error (C G D F).  Cause . . . . . :  RPG procedure BASICF in program SPACE01/BASICF at  statement 51 had an error due to a pointer not being correctly set. The  cause of the error is most likely one of the following:  * A basing pointer was not set.  * A procedure pointer was not set.  * The pointer was set, but the object it referenced has been destroyed.  * A parameter was not passed to the program containing the procedure.  * A parameter was not passed to the procedure by its caller within the program.  * A pointer offset was greater than the size of the space the pointer was pointing to.
Remember When you are using pointers . . .  You are indiscriminately playing with memory. You must be careful.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++++ D pPointer  S  *  D BasedField  S  20  Based(pPointer)  Pointers in RPG IV ,[object Object],[object Object],[object Object],[object Object]
Pointers at work Storage of Program ,[object Object],BasedField 1
Pointers at work Storage of Program 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],BasedField 1 4 3 5
Using pointers DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++++ D pPointer  S  *  Inz(%Addr(Text1))  D BasedField  S  5  Based(pPointer)  * &quot;Overlays&quot; Text1  D MyDS  DS D  Text1  4  Inz('ABCD')  D  Text2  4  Inz('WXYZ')  C  Eval  pPointer = %Addr(Text2)  * Now, BasedField &quot;Overlays&quot; Text2  ,[object Object],[object Object],[object Object],[object Object]
Why use pointers? * When allocating record layouts in trigger programs. * Used with C functions. * Dynamic memory allocation. * Used with many APIs - e.g., user spaces * As a way of calling procedures.
Standard trigger buffer D TriggerBuffer  DS  Qualified D  FileName  10  D  LibraryName  10  D  MemberName  10  D  Event  1  D  Time  1  D  CommitLock  1  D  Fill01  3  D  CCSID  10I 0  D  RRN  10I 0  D  Fill02  4  D  OldOffset  10I 0  D  OldLength  10I 0  D  OldNullOff  10I 0  D  OldNullLen  10I 0  D  NewOffset  10I 0  D  NewLength  10I 0  D  NewNullOff  10I 0  D  NewNullLen  10I 0  * Before and After Images  Offset to New Record Offset to Original Record
D OldFile  E DS  ExtName(FileName)  D  Qualified  D NewFile  E DS  ExtName(FileName)  OldFile = %SubST(TriggerBuffer:  TriggerBuffer.OldOffset+1:  TriggerBuffer.OldLength);  NewFile = %SubST(TriggerBuffer:  TriggerBuffer.NewOffset+1:  TriggerBuffer.NewLength)  Accessing the Trigger Buffer The %SUBST Built-in Function can be used to copy the contents of the Trigger Buffer to the relevent externally described data structures.
D OldFile  E DS  ExtName(FileName)  D  Qualified  D  Based(OldPtr)  D NewFile  E DS  ExtName(FileName)  D  Based(NewPtr)  D OldPtr  S  *  D NewPtr  S  *  OldPtr = %Addr(TriggerBuffer) + TriggerBuffer.OldOffset NewPtr = %Addr(TriggerBuffer) + TriggerBuffer.NewOffset Using pointers ,[object Object],[object Object]
C functions D GetToken  Pr  *  ExtProc('strtok') D  pString  *  Value Options(*String) D  pDelimiters  *  Value Options(*String) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
D SortIt  PR  ExtProc('qsort')  D  DataStart  *  Value  D  Elements  10U 0 Value  D  Size  10U 0 Value  D  CompFunc  *  ProcPtr Value  D FindIt  PR  *  ExtProc('bsearch')  D  LookFor  *  Value  D  DataStart  *  Value  D  Elements  10U 0 Value  D  Size  10U 0 Value  D  CompFunc  *  ProcPtr Value  Other C functions ,[object Object],[object Object]
Dynamic Memory Allocation D Array  S  10  Based(pArray) Dim(10000) D pArray  S  *  D MaxElem  S  10U 0 Inz(10)  /Free  pArray = %Alloc(%Size(Array) * MaxElem);  MaxElem = MaxElem + 10;  pArray = %ReAlloc(pArray: %Size(Array) * MaxElem);  DeAlloc  pArray;  *InLR = *On;  /End-Free  ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example of Dynamic Memory Allocation D PathArray  S  *  Dim(32767)  D  Based(pPathArray)  // Allocate memory for the array of pointers  pPathArray = %alloc((%size(pDataPtr) * NumberInList));  // Build the array of pointers to the path entries  for i = 1 to NumberInList;  j = j + 1;  If (j > %Elem(PathArray));  pPathArray = pPathArray +  (%Size(pDataPtr) * %Elem(PathArray));  j = 1;  EndIf;  PathArray(j) = pDataPtr;  This example shows the use of dynamic memory allocation for an array that may have more then 32767 elements. The array is “repositioned” every 32767 elements.
Example of Dynamic Memory Allocation PathArray 1. NumberInList = 70,000 %Size(PDataPtr) = 16 2. PathArray pPathArray = address of allocated memory 3. Program loops through 32767 elements of PathArray 4. “Move” the position of the array pPathArray = pPathArray +  (%Size(pDataPtr) * %Elem(PathArray)); 5. etc. < 1 ---------------- 524,272 > < 524,273------------ 1,048,544 > < 1,048,545 --------- 1,572,816 > 5. PathArray 4. PathArray %Alloc assigns 1,120,000 bytes 2. pPathArray= %alloc((%size(pDataPtr)*NumberInList)); < 1 ----------------------------------------------------- 1,120,000 >
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],What is a user space?
User space APIs ,[object Object],[object Object],[object Object],[object Object],[object Object]
Create user space D CreateSpace  PR  ExtPgm('QUSCRTUS') D  UserSpaceName  20  Const D  Attribute  10  Const D  Size  10I 0 Const D  Initial  1  Const D  Authority  10  Const D  Text  50  Const  * Optional Parameter Group 1 D  Replace  10  Const Options(*NOPASS) D  ErrorCode  Const Options(*NOPASS) D  Like(StandardAPIError) * Optional Parameter Group 2 D  Domain  10  Const Options(*NOPASS) * Optional Parameter Group 3 D  TransferSize  10I 0 Const Options(*NOPASS) D  OptimumAlign  1  Const Options(*NOPASS) CreateSpace(UserSpace:'DTA':10000: X'00':'*ALL':Text);  The QUSCRTUS API is called to create a user space
Create user space DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++++ D  UserSpace  S  20  Inz('USERSPACE *CURLIB')  D  Attribute  S  10  Inz('DTA')  D  Size  S  10I 0 Inz(10000)  D  Initial  S  1  Inz(X'00')  D  Authority  S  10  Inz('*ALL')  D  Text  S  50  Inz('Sample User Space')  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Fill user space D CatData  E DS  ExtName(Category) D NumRows  S  5I 0 Based(pNumRows) D pNumRows  S  * D CatSpace  S  Dim(32767) Based(pCatSpace) D  Like(CatData) D pCatSpace  S  * ,[object Object],[object Object],[object Object],[object Object]
Fill user space D GetSpace  PR  ExtPgm('QUSPTRUS') D  SpaceName  20  Const D  pSpacePtr  * * Optional Parameter Group  D  ErrorCode  Const Options(*NOPASS) D  Like(StandardAPIError) D  UserSpace  S  20  Inz('CATEGORY  *LIBL  ') GetSpace(SpaceName:pNumRows); pCatSpace = pNumRows + %Size(NumRows); ,[object Object],[object Object],[object Object],[object Object],[object Object]
Fill user space NumRows = 0; Read Category; DoW Not %EOF(Category);  NumRows = NumRows + 1; CatSpace(NumRows) = CatData;  Read Category;  EndDo;   We have a loop that fills the user space by reading a record from the category file and adding it to the next element of the array.
Using the user space D CatData  E DS  ExtName(Category) D NumRows  S  5I 0 Based(pNumRows) D pNumRows  S  * D CatSpace  S  Dim(32767) Based(pCatSpace) D  Like(CatData) D pCatSpace  S  * ,[object Object],[object Object],[object Object]
Using the user space // Obtain pointers to the user space GetSpace(SpaceName:pNumRows); pCatSpace = pNumRows + %Size(NumRows); ,[object Object],[object Object]
Using the user space For RRN = 1 To NumRows; CatData = CatSpace(RRN);  Write SubRec;  EndFor;   ,[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],APIs and user spaces D ListObjects  PR  ExtPgm('QUSLOBJ') D  UserSpace  20  Const D  Format  8  Const D  Objects  20  Const D  ObjectType  10  Const D  ErrorCode  Like(StandardAPIError) CreateSpace('MYSPACE  QTEMP  ':'DTA':10000: X'00':'*ALL':  'All Objects in MYLIB');  ListObjects('MYSPACE  QTEMP  ':'OBJL0100':  '*ALL  MYLIB  ':'*ALL  ':APIError);
The user space APIs are documented in the Information Center under Programming->APIs->APIs by Category->Object APIs.  The use of user spaces for other APIs is detailed in the documentation of the individual APIs (like QUSLOBJ) API documentation
Backup and Recovery APIs Office APIs Client Management Support APIs Operational Assistant APIs Communications APIs Performance Collector APIs Configuration APIs Print APIs Debugger APIs Problem Management APIs Dynamic Screen Manager APIs Program and CL Command APIs Edit Function APIs Registration Facility APIs File APIs Remote Procedure Call APIs Hardware Resource APIs Security APIs Hierarchical File System APIs Server Support APIs High-Level Language APIs Software Product APIs ILE CEE APIs UNIX-Type APIs Journal and Commit APIs User Interface APIs Message Handling APIs Virtual Terminal APIs National Language Support APIs Work Management APIs Network Management APIs Work Station Support APIs Object APIs Miscellaneous APIs Categories of APIs Each has its own section, so pick a topic and while away a weekend in perusal.
Procedure pointers D pGenProc  S  *  ProcPtr D GenProc  PR  30  ExtProc(pGenProc) pGenProc = %PAddr('Proc01'); Returned = GenProc(); pGenProc = %PAddr('AnotherProcAltogether'); Returned = GenProc(); ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Any clearer now? Basing pointers are used to locate the storage for based variables. The storage is accessed by defining a field, array, or data structure as based on a particular basing pointer variable and setting the basing pointer variable to point to the required storage location.
Check out the RPG IV Redbook Who Knew You Could Do That with RPG IV? A Sorcerer's Guide to System Access and More SG24-5402 International Technical Support Organization Rochester, Minnesota ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Summary  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

Network Hardware And Software
Network Hardware And SoftwareNetwork Hardware And Software
Network Hardware And SoftwareSteven Cahill
 
Firewalls and packet filters
Firewalls and packet filtersFirewalls and packet filters
Firewalls and packet filtersMOHIT AGARWAL
 
Office automation system using arduino
Office automation system using arduinoOffice automation system using arduino
Office automation system using arduinoAshfaqul Haque John
 
LAN (Local Area Network)
LAN (Local Area Network)LAN (Local Area Network)
LAN (Local Area Network)Ridwanul Hoque
 
Mobile Phone Seizure Guide by Raghu Khimani
Mobile Phone Seizure Guide by Raghu KhimaniMobile Phone Seizure Guide by Raghu Khimani
Mobile Phone Seizure Guide by Raghu KhimaniDr Raghu Khimani
 
Pds m series-traditional_io[1]
Pds m series-traditional_io[1]Pds m series-traditional_io[1]
Pds m series-traditional_io[1]Heirarch Meissner
 
CMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signatureCMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signatureAdarsh Patel
 
Wireless body area network
Wireless body area networkWireless body area network
Wireless body area networkPratik Gondaliya
 
Steganography and its techniques
Steganography and its techniquesSteganography and its techniques
Steganography and its techniquesFatema Panvelwala
 
Networking basic fundamental
Networking basic fundamentalNetworking basic fundamental
Networking basic fundamentalSatish Sehrawat
 
TELEPHONE CONVERSATION RECORDING
TELEPHONE CONVERSATION RECORDINGTELEPHONE CONVERSATION RECORDING
TELEPHONE CONVERSATION RECORDINGAlapati Krishna
 
Intrusion detection system ppt
Intrusion detection system pptIntrusion detection system ppt
Intrusion detection system pptSheetal Verma
 
Bar codes and its type with different scanner and its application
Bar codes and its type with different scanner and its applicationBar codes and its type with different scanner and its application
Bar codes and its type with different scanner and its applicationAkshay Shelake
 
OPC UA mit der SIMATIC S7-1200/1500 und Python
OPC UA mit der SIMATIC S7-1200/1500 und PythonOPC UA mit der SIMATIC S7-1200/1500 und Python
OPC UA mit der SIMATIC S7-1200/1500 und PythonJohannes Kinzig
 
Types of cctv cameras
Types of cctv camerasTypes of cctv cameras
Types of cctv camerasTanmayDivekar
 
Body Area Network Presentation
Body Area Network PresentationBody Area Network Presentation
Body Area Network PresentationNishant Kumar
 

La actualidad más candente (20)

Cryptography and Steganography
Cryptography and SteganographyCryptography and Steganography
Cryptography and Steganography
 
Network Hardware And Software
Network Hardware And SoftwareNetwork Hardware And Software
Network Hardware And Software
 
Exposicion sistemas integrados de seguridad
Exposicion sistemas integrados de seguridadExposicion sistemas integrados de seguridad
Exposicion sistemas integrados de seguridad
 
Firewalls and packet filters
Firewalls and packet filtersFirewalls and packet filters
Firewalls and packet filters
 
Network Topology
Network TopologyNetwork Topology
Network Topology
 
Office automation system using arduino
Office automation system using arduinoOffice automation system using arduino
Office automation system using arduino
 
LAN (Local Area Network)
LAN (Local Area Network)LAN (Local Area Network)
LAN (Local Area Network)
 
Mobile Phone Seizure Guide by Raghu Khimani
Mobile Phone Seizure Guide by Raghu KhimaniMobile Phone Seizure Guide by Raghu Khimani
Mobile Phone Seizure Guide by Raghu Khimani
 
Pds m series-traditional_io[1]
Pds m series-traditional_io[1]Pds m series-traditional_io[1]
Pds m series-traditional_io[1]
 
CMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signatureCMACs and MACS based on block ciphers, Digital signature
CMACs and MACS based on block ciphers, Digital signature
 
Wireless body area network
Wireless body area networkWireless body area network
Wireless body area network
 
Steganography and its techniques
Steganography and its techniquesSteganography and its techniques
Steganography and its techniques
 
Networking basic fundamental
Networking basic fundamentalNetworking basic fundamental
Networking basic fundamental
 
TELEPHONE CONVERSATION RECORDING
TELEPHONE CONVERSATION RECORDINGTELEPHONE CONVERSATION RECORDING
TELEPHONE CONVERSATION RECORDING
 
Intrusion detection system ppt
Intrusion detection system pptIntrusion detection system ppt
Intrusion detection system ppt
 
Bar codes and its type with different scanner and its application
Bar codes and its type with different scanner and its applicationBar codes and its type with different scanner and its application
Bar codes and its type with different scanner and its application
 
OPC UA mit der SIMATIC S7-1200/1500 und Python
OPC UA mit der SIMATIC S7-1200/1500 und PythonOPC UA mit der SIMATIC S7-1200/1500 und Python
OPC UA mit der SIMATIC S7-1200/1500 und Python
 
Types of cctv cameras
Types of cctv camerasTypes of cctv cameras
Types of cctv cameras
 
Body Area Network Presentation
Body Area Network PresentationBody Area Network Presentation
Body Area Network Presentation
 
Cctv components
Cctv componentsCctv components
Cctv components
 

Similar a Rpg Pointers And User Space

Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstackThierry Gayet
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Open source report writing tools for IBM i Vienna 2012
Open source report writing tools for IBM i  Vienna 2012Open source report writing tools for IBM i  Vienna 2012
Open source report writing tools for IBM i Vienna 2012COMMON Europe
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdfssusere19c741
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
C intro
C introC intro
C introKamran
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptxRohitRaj744272
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 

Similar a Rpg Pointers And User Space (20)

L7 pointers
L7 pointersL7 pointers
L7 pointers
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Open source report writing tools for IBM i Vienna 2012
Open source report writing tools for IBM i  Vienna 2012Open source report writing tools for IBM i  Vienna 2012
Open source report writing tools for IBM i Vienna 2012
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
C
CC
C
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
C intro
C introC intro
C intro
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
C programming
C programmingC programming
C programming
 

Rpg Pointers And User Space

  • 1. Coming to Grips with Pointers and User Spaces in RPG IV Phone: +91 9971458335 e-Mail: ramanjosan@yahoo.com Ramandeep Josan
  • 2. Ramandeep Josan Ramandeep Josan has worked in the development of IBM Midrange applications for 8+ years and 3 years on Java using J2ME technology. He is Project Management professional (PMBOK4 - from PMI) and Sun Java certified. He is Project manager for Satyam and has worked for various banking organizations across the globe.
  • 3.
  • 4. What is a pointer? Basing pointers are used to locate the storage for based variables. The storage is accessed by defining a field, array, or data structure as based on a particular basing pointer variable and setting the basing pointer variable to point to the required storage location.
  • 5.
  • 6.
  • 7. Parameter lists BProgram(I: J: K); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> D BProgram PI D X 15 D Y 10 D Z 5 Storage of Calling Program I J K Storage of Called Program X Y Z
  • 8. The problem with pointers BProgram(I: J: K); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> D BProgram PI D X 15 D Y 10 D Z 15 Z = *Blanks; Storage of Calling Program I J K Storage of Called Program X Y Z Oops!
  • 9. The problem with pointers BProgram(I: J: K); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> D BProgram PI D X 15 D Y 10 D Z 15 Z = *Blanks; Storage of Calling Program I J K Storage of Called Program X Y Z Oops!
  • 10. Better get used to . . . Message ID . . . . . . : RNQ0222 Date sent . . . . . . : 13/09/02 Time sent . . . . . . : 11:40:31 Message . . . . : Pointer or parameter error (C G D F). Cause . . . . . : RPG procedure BASICF in program SPACE01/BASICF at statement 51 had an error due to a pointer not being correctly set. The cause of the error is most likely one of the following: * A basing pointer was not set. * A procedure pointer was not set. * The pointer was set, but the object it referenced has been destroyed. * A parameter was not passed to the program containing the procedure. * A parameter was not passed to the procedure by its caller within the program. * A pointer offset was greater than the size of the space the pointer was pointing to.
  • 11. Remember When you are using pointers . . . You are indiscriminately playing with memory. You must be careful.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. Why use pointers? * When allocating record layouts in trigger programs. * Used with C functions. * Dynamic memory allocation. * Used with many APIs - e.g., user spaces * As a way of calling procedures.
  • 17. Standard trigger buffer D TriggerBuffer DS Qualified D FileName 10 D LibraryName 10 D MemberName 10 D Event 1 D Time 1 D CommitLock 1 D Fill01 3 D CCSID 10I 0 D RRN 10I 0 D Fill02 4 D OldOffset 10I 0 D OldLength 10I 0 D OldNullOff 10I 0 D OldNullLen 10I 0 D NewOffset 10I 0 D NewLength 10I 0 D NewNullOff 10I 0 D NewNullLen 10I 0 * Before and After Images Offset to New Record Offset to Original Record
  • 18. D OldFile E DS ExtName(FileName) D Qualified D NewFile E DS ExtName(FileName) OldFile = %SubST(TriggerBuffer: TriggerBuffer.OldOffset+1: TriggerBuffer.OldLength); NewFile = %SubST(TriggerBuffer: TriggerBuffer.NewOffset+1: TriggerBuffer.NewLength) Accessing the Trigger Buffer The %SUBST Built-in Function can be used to copy the contents of the Trigger Buffer to the relevent externally described data structures.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23. Example of Dynamic Memory Allocation D PathArray S * Dim(32767) D Based(pPathArray) // Allocate memory for the array of pointers pPathArray = %alloc((%size(pDataPtr) * NumberInList)); // Build the array of pointers to the path entries for i = 1 to NumberInList; j = j + 1; If (j > %Elem(PathArray)); pPathArray = pPathArray + (%Size(pDataPtr) * %Elem(PathArray)); j = 1; EndIf; PathArray(j) = pDataPtr; This example shows the use of dynamic memory allocation for an array that may have more then 32767 elements. The array is “repositioned” every 32767 elements.
  • 24. Example of Dynamic Memory Allocation PathArray 1. NumberInList = 70,000 %Size(PDataPtr) = 16 2. PathArray pPathArray = address of allocated memory 3. Program loops through 32767 elements of PathArray 4. “Move” the position of the array pPathArray = pPathArray + (%Size(pDataPtr) * %Elem(PathArray)); 5. etc. < 1 ---------------- 524,272 > < 524,273------------ 1,048,544 > < 1,048,545 --------- 1,572,816 > 5. PathArray 4. PathArray %Alloc assigns 1,120,000 bytes 2. pPathArray= %alloc((%size(pDataPtr)*NumberInList)); < 1 ----------------------------------------------------- 1,120,000 >
  • 25.
  • 26.
  • 27. Create user space D CreateSpace PR ExtPgm('QUSCRTUS') D UserSpaceName 20 Const D Attribute 10 Const D Size 10I 0 Const D Initial 1 Const D Authority 10 Const D Text 50 Const * Optional Parameter Group 1 D Replace 10 Const Options(*NOPASS) D ErrorCode Const Options(*NOPASS) D Like(StandardAPIError) * Optional Parameter Group 2 D Domain 10 Const Options(*NOPASS) * Optional Parameter Group 3 D TransferSize 10I 0 Const Options(*NOPASS) D OptimumAlign 1 Const Options(*NOPASS) CreateSpace(UserSpace:'DTA':10000: X'00':'*ALL':Text); The QUSCRTUS API is called to create a user space
  • 28.
  • 29.
  • 30.
  • 31. Fill user space NumRows = 0; Read Category; DoW Not %EOF(Category); NumRows = NumRows + 1; CatSpace(NumRows) = CatData; Read Category; EndDo; We have a loop that fills the user space by reading a record from the category file and adding it to the next element of the array.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36. The user space APIs are documented in the Information Center under Programming->APIs->APIs by Category->Object APIs. The use of user spaces for other APIs is detailed in the documentation of the individual APIs (like QUSLOBJ) API documentation
  • 37. Backup and Recovery APIs Office APIs Client Management Support APIs Operational Assistant APIs Communications APIs Performance Collector APIs Configuration APIs Print APIs Debugger APIs Problem Management APIs Dynamic Screen Manager APIs Program and CL Command APIs Edit Function APIs Registration Facility APIs File APIs Remote Procedure Call APIs Hardware Resource APIs Security APIs Hierarchical File System APIs Server Support APIs High-Level Language APIs Software Product APIs ILE CEE APIs UNIX-Type APIs Journal and Commit APIs User Interface APIs Message Handling APIs Virtual Terminal APIs National Language Support APIs Work Management APIs Network Management APIs Work Station Support APIs Object APIs Miscellaneous APIs Categories of APIs Each has its own section, so pick a topic and while away a weekend in perusal.
  • 38.
  • 39. Any clearer now? Basing pointers are used to locate the storage for based variables. The storage is accessed by defining a field, array, or data structure as based on a particular basing pointer variable and setting the basing pointer variable to point to the required storage location.
  • 40.
  • 41.

Notas del editor

  1. You don&apos;t actually have to define Pointers. If the compiler comes across a pointer in a Based keyword, it will automatically define it for you - so watch the spelling! The only time you need to be concerned about the length of a pointer is when is is defined in a data structure - check out the ALIGN keyword for further details.
  2. The nearest that traditional programming can come to this concept is a data area, but you have to input and output data areas to and from programs. In contrast, you can immediately change the content of a user space by merely changing the value of a field in your program.
  3. CatData is an externally defined data structure that uses the table being copied to the user space for the external definition. When a record is read from the file it is automatically placed in this data structure. NumRows is the integer that contains the count of the number of rows. It will be mapped to the starting position of the user space. pNumRows is the basing pointer for NumRows . CatSpace is an array where each element contains an image of a record. It will be mapped to the remainder of the user space. Note that the array is defined with the maximum number of elements (32767) so you must ensure that the program does not try to reference an element that is beyond the actual size of the user space. Since the array is based on a pointer (CATPTR), no storage is allocated for the array; therefore, the size allocated would be the same if you had defined the array with 50 elements. pCatSpace is the basing pointed for CatSpace .
  4. The program starts by calling QUSPTRUS to load the user space and retrieve the pointer to it. The pointer for the array of rows is calculated as the starting pointer ( pNumRows ) plus the size of the based field ( NumRows ).
  5. NumRows is used as the index for the array. A loop is used to perform a sequential read of the table and for every record the data structure (into which the record is automatically placed) is copied to the next element of the loop. At end of file NumRows will reflect the number of rows copied. The user space now contains an image of the rows in the table along with a field that indicates how many rows there are. A sleight word of warning; if you need to perform a LOOKUP operation on an array in a user space ensure that you use one of the %LOOKUP BIFs and that you specify the optional number of elements parameter. Remember that the definition of the array in the program will more then likely exceed the actual number of elements in the user space.