Unconfigured Ad Widget

Collapse

Announcement

Collapse
No announcement yet.

TechTip: RPG IV Pointers--They're Easy!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TechTip: RPG IV Pointers--They're Easy!

    ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
    This is a discussion about TechTip: RPG IV Pointers--They're Easy!.

    Click here for the article.


  • #2
    TechTip: RPG IV Pointers--They're Easy!

    ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
    I've always understood how to use pointers I'm just not clear on what the advantage is to using them.

    Comment


    • #3
      TechTip: RPG IV Pointers--They're Easy!

      ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
      Hello Bill, One recent example is to be able to call a procedure that will fill an external data structure with data and by simply passing a pointer to the data sturcture you can access the data in your calling program, or pass the data to other programs/procedure without having to pass the entire data structure. Just be careful because the data in the data structure can be manipulated in any program/procedure it is passed to. This technique can be used to separate the data retrieval logic from the business logic. Mike
      Code

      Comment


      • #4
        TechTip: RPG IV Pointers--They're Easy!

        ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
        Hi to all. This is my question (about pointers): I've 3 vars: (Numeric) Num1 = 100 Num2 = 200 Num3= 300 Idx = 1 Result = *zeros I've 1 var: (Char 10) NamVar = 'Num' + %char(Idx) = 'Num1' ok, now, how can I retrieve a var Result throws NamVar? I want: Result = (something)NamVar = 100 Is it possible in RPG? Thanks. Victor.

        Comment


        • #5
          TechTip: RPG IV Pointers--They're Easy!

          ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
          Bill, One advantage is saving space in your program: Everyone has had the experience of needing an array but never knowing how many elements to allocate for. In the bad old days of RPG-III you'd simply specify some huge number and leave it at that. The problem with this is that the storage that the system allocates to maintain such things is huge and takes up unnecessary resources. The solution to this is pointers and the %ALLOC() & %REALLOC() built-ins. Once the program determines the number of elments needed it can simply allocate (or re-allocate) the necessary storage. Another advantage of pointers is speed: Some time ago I had a project that required continually looping through the same set of data over and over again. Every time the program ran it didn't know how much data it would have to run through. I could have used a series of work files, but I decided to use a series of dynamic arrays and allocate memory for them, which involved the use of pointers. The program plows repeatedly through thousands of records every morning in only ten minutes. I estimate that if I had used work files the amount of I/O involved would have required two hours or more of processing. Another advantage is single point of maintenance and code reuse: Subfiles are a pain. There is a whole host of stuff you have to do in every program so that they'll work properly and be easy to use. It would be wonderful if one could put all that code in an external procedure, but the problem the subfile interfaces are intertwined with your program and can't be easily offloaded to a service program. Things like I/O to files or the subfile must be done in your program and the variables to control the subfile are all local can't be easily exported. I solved this problem with pointers. For every subfile the service program allocates a space where it stores pointers to the program's internal subfile control variables. To do the proper I/O it also stored pointers to procedures in the main program that are called when it needs to read a record or write something to the subfile. (In Java this would be called an Abstract Class.) Another advantage is generic processing: We have a web page that lists the statuses of active orders in the system. Different customers wanted different data in the columns and wanted the columns in different positions. Of course each column had to be sortable. Not only would this involve dynamic arrays, but one would need pointer to access each column in the data structure. There are probably many more examples out there that I haven't even thought of. If you (or anyone else) wants a more in-depth explanation then drop me an email.

          Comment


          • #6
            TechTip: RPG IV Pointers--They're Easy!

            ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
            Thanks for the article Jim. I use pointers frequently to pass array addresses to various procedures which manipulate the array for use in the calling module. However, I've never really understood why I would need to use a procedure pointer. Do you have a really good example of this? Thanks.

            Comment


            • #7
              TechTip: RPG IV Pointers--They're Easy!

              ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
              1. Any repeating structure can easily be traversed by using a data structure based on a pointer. Applying this concept to array "structures" will give you the ability to create arrays of virtually unlimited size. 2. This is more of a side effect of using pointers but if you have a good grasp of pointers and how to use them then you are more likely to make the correct choices when creating procedures and prototypes. I speak in regard to passing by value or by reference or by read only reference. I find that developers that have a solid understanding of pointers are more likely to have a good grasp on when and how to use the different types of parms. 3. APIs. Almost all of IBMs APIs use pointers.

              Comment


              • #8
                TechTip: RPG IV Pointers--They're Easy!

                ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
                In your example you have the following code... Size = 1000 * %len(Array); // Size becomes 10,000 This will cause a compiler error because there is no specific "length" for Array. I think what you meant is... Size = 1000 * %size(Array); // Size becomes 10,000 HTH, Jeff

                Comment


                • #9
                  TechTip: RPG IV Pointers--They're Easy!

                  ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
                  Jeff, You are entirely correct! I use %len so much that I mistakenly used it instead of %size, as I should have. Good catch, and thanks for the correction. It's good to know that some people actually try out these tips. Jim

                  Comment


                  • #10
                    TechTip: RPG IV Pointers--They're Easy!

                    ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
                    Say you have a BASED array that you have defined and allocated some of the storage for it. You can't use SORTA to sort that beast because not all of the elements have storage allocated for them. You have a couple of options. You can write a simple bubble sort to sort the portion of the array that you have allocated, or you can use the qsort function from the C library. Qsort needs to know how to compare two elements of an array, so you write a procedure that compares two elements and returns -1 if the first is less than the second, 0 if they are equal, and 1 if the first is greater than the second. You pass a procedure pointer to qsort, and now it knows how to sort your array, regardless of how complex the structure is. And as an additional benefit, qsort is much faster than a bubble sort.

                    Comment


                    • #11
                      TechTip: RPG IV Pointers--They're Easy!

                      ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
                      Victor, I have two responses for your inquiry. My first response is to do the following: Instead of using fields that have a number suffix, instead define an array (call it Num), with as many elements as you need. Load the values into the array using whatever process fits - maybe a loop, maybe not. When you need to get Result, use the assignment statement Result = Num(Idx); In this solution there is no need for NamVar. If your circumstances do not permit this solution, then consider my second solution coded and documented below. It's a longer solution. To answer your question specifically, there is no way to get the contents of a field, at run time, from its name. That is why I pre-load field addresses in the second solution. I hope you can use one of these two alternatives. Jim
                      Code

                      Comment


                      • #12
                        TechTip: RPG IV Pointers--They're Easy!

                        ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
                        You can certainly use SORTA on a dynamically allocated array on V5R3. sorta %subarr(MyArray:Start:End) ; Chris

                        Comment


                        • #13
                          TechTip: RPG IV Pointers--They're Easy!

                          ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
                          Jim: The pointer data type and based variables is another item I was responsible for implementing back in V3R1. For that sin, I have elsewhere offered my sincerest apologies. But then again, if I didn't implement it, someone else would have. There are a couple of problems with pointers as implemented in RPG. The most egregious is that they're untyped. That is, the compiler can't do any verification at all with respect to the data types involved in any pointer operation. Compare this with C. If you declare a pointer variable as say "int *p;", then the operation "p=&x;" is valid only if "x" is defined as "int". In RPG, a pointer can be assigned the address of anything, no matter what the type. RPG will quite happily let you do things that may well cause you hours of debugging. C will let you know of the problem at compile time. My recommendation is that if you think you have to use pointers in your RPG programming, use C instead. Cheers! Hans

                          Comment


                          • #14
                            TechTip: RPG IV Pointers--They're Easy!

                            ** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
                            I requested that MC Press fix my goof, using %len instead of %size, so it should be fixed now. Jim

                            Comment

                            Working...
                            X