ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Pass PROCPTR using VALUE/CONST

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

  • Pass PROCPTR using VALUE/CONST

    Hi.

    I know the difference between passing parameters by VALUE and by CONST (read-only reference) so that's not what this question is asking.

    I, as well as many of you, use Scott Klement's code for HTTP and DATA-xxx. I noticed in the source that all PROCPTR parameters are being passed by VALUE.

    Is there a problem passing a PROCPTR parameter using CONST? I've used CONST plenty of times when passing a PROCPTR parameter and never had a problem.

    Thanks in advance,

    Mike
    Last edited by mlopez01; 3 weeks ago.

  • #2
    Passing by CONST reference should work fine. I don't really understand why you'd want to do that, how does it help you to pass it by CONST? But... I don't see any reason why it wouldn't work.

    Comment


    • #3
      Hi, Scott.

      I'm old and worked during the days when memory and CPU cycles were a commodity (DOS, C/C++, etc.) and discipline was a necessity, so I'm used to conserving both. Passing an address was/is more efficient than creating a temporary variable and copying memory contents.

      It has carried over to today.

      Mike

      Comment


      • #4
        Originally posted by mlopez01 View Post
        I'm old and worked during the days when memory and CPU cycles were a commodity (DOS, C/C++, etc.) and discipline was a necessity, so I'm used to conserving both. Passing an address was/is more efficient than creating a temporary variable and copying memory contents.

        It has carried over to today.
        You aren't conserving them, you're wasting them. It doesn't matter because it's a tiny amount of CPU and memory, but... your way is less efficient. If you pass a pointer by reference, under the covers, what you are doing is passing a pointer to the pointer. That requires an extra step for the computer vs. just passing the pointer in the first place.

        Since you code in C/C++ it's pretty obvious when you look at the code in C. Here is your way of passing a pointer by const reference:

        Code:
        void myfunc(const char **stringptr) {
          const char *string = *stringptr;
          if (strcmp(string, "X") == 0) {
            /* stuff */
          }
        }
        ​​
        Here is passing a pointer by value:

        Code:
        void myfunc(const char *string) {
          if (strcmp(string, "X") == 0) {
            /* stuff */
          }
        }

        The code is identical except for one extra line of code that has to execute, and one extra variable declared. The extra line of code and extra variable mean more CPU cycles and more memory used. Passing a pointer by value is more efficient.

        It's even more obvious if you look at what's happening in down at the assembly or machine code layer. In BOTH cases you are placing 16 bytes of data on the stack to pass to the procedure. In both cases a memory address is being passed on the stack. The only difference is that when you pass by reference that 16 bytes has to be dereferenced to another memory address. Whereas when you pass it by value you can skip that step.

        Your idea that passing by reference is always more efficient is simply not true. When you pass by reference the address of the variable you're passing is placed on the stack -- to put it another way, 16 bytes are passed by value. When you pass by value instead, the number of bytes is equal to what you are passing. So if you pass data larger than 16 bytes, then YES, passing by reference is faster. But if it's <= 16 bytes, passing by value is faster.

        If you take a class in assembly programming and learn about calling routines in assembly, this will become quite obvious.

        But again... we're talking about TINY amounts of memory and performance difference here. Even though my way is slightly faster than yours, the difference won't matter in the real world. So you should code whatever makes your program easier to maintain. In your example, however, I think they're equallty easy to maintain, so it's really a non-issue.

        Comment


        • #5
          When I mentioned C/C++ I was referencing programming and computer hardware/software circa 1990, so at one point I was working with 200k of ram at most. I no longer code in C/C++ (though I do love those languages).

          So passing by value back then put a lot of pressure on memory since memory had to be allocated that matched the parameter's size and then memory copied. That was expensive. I'm talking about types other than pointers (e.g. char).

          So in today's ILE RPG passing a character parameter by reference is passing 16 bytes which, if larger than the declared character variable size, is not as efficient. Agreed it's negligible. If a character variable is declared to be 1MB though, I'm passing by reference.

          Comment


          • #6
            Originally posted by mlopez01 View Post
            So in today's ILE RPG passing a character parameter by reference is passing 16 bytes which, if larger than the declared character variable size, is not as efficient. Agreed it's negligible. If a character variable is declared to be 1MB though, I'm passing by reference.
            I agree with you on that. And I sure hope everyone is passing their 1 MB character variable by reference!

            But at the start of this thread, you were talking about procedure pointers by reference vs by value. In the case of passing a pointer (of any type), it's always more efficient to pass it by value.

            Comment


            • #7
              Agreed.

              Comment

              Working...
              X