Thread: Parameter passing with pointer to pointer

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Parameter passing with pointer to pointer

    I have a disagreement with people from work, they say if you pass a pointer to a pointer, that is pass by value. But it is obviously pass by reference, not? I want to learn.

    Please look at this signature which is the example:
    Code:
    void doSomething(std::string** ppStr)
    They say it's pass by value! I explained like below which I think is enough but they refuse understand; please give a honest view if the following explanation is correct:


    Code:
    void doSomething(std::string** ppStr)
    This is not only pass by reference, it is the very truthful pass by reference, more thruthful than pass by reference using its reference type.
    The most truthfull pass by reference, the one that can null the referenced, is pass by address, the signature would be pass by pointer, what's passed is an address.
    Pass pointer to pointer is exactly pass by address, so it is pass by reference in the most extreme sense.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm afraid your workmates are right.

    Passing by value or by reference is analyzed from the perspective of the parameter of your function. And it is simply the result of the argument being copied or not during the parameter initialization.

    The reason why C doesn't have pass-by-reference is exactly because it doesn't have references, it only has pointers. And pointers are always copied by value. What happens is simply that when you have a function accepting a pointer parameter, that parameter will be initialized with a copy of the argument. So in fact you will have a new pointer inside your function. Changing where that pointer points to, will not change the pointer used as an argument.

    And this is true also for pointers to pointers.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    The pointer is passed by value, the string being referenced is the same though. Try it!
    Code:
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    
    
    void doSomething(std::string** ppStr) {
      **ppStr = "modified";
      ppStr = 0;
    }
    
    int main() {
      std::string** ptr = &(new std::string("original"));
      std::cout << **ptr << std::endl;
    
      doSomething(ptr);
      std::cout << **ptr << std::endl;
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Both sides can be considered correct depending on what you are referring to. In the original function, a pointer to a pointer to a string is passed by value. A pointer to a string is passed be reference. I guess technically the string is passed by reference also, although to pass the string by reference you would usually use string* or string&.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Daved
    Both sides can be considered correct depending on what you are referring to. In the original function, a pointer to a pointer to a string is passed by value. A pointer to a string is passed be reference. I guess technically the string is passed by reference also, although to pass the string by reference you would usually use string* or string&.
    Can you please elaborate Daved?

    If I alter to where the pointer points inside the function, that change will not affect the original pointer used as an argument. The pointer was copied.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If I alter to where the pointer points inside the function, that change will not affect the original pointer used as an argument. The pointer was copied.

    What are you referring to? In the case of string**, the string** is passed by value- a copy is made, and if you change where the double pointer points to it will not effect the original double pointer from the calling code. In Perspective's code, the ppStr = 0 does not affect the ptr variable in main. That is why I said that the pointer to a pointer to a string is passed by value.

    However, the single pointer that the double pointer refers to is passed by reference (via a pointer). So if you used the code *ppStr = 0 inside the function, it will affect the pointer passed to it. In Perspective's example, if the second line of doSomething had been *ppStr = 0; then ptr inside main would have been affected and would now be a pointer to a null pointer. The memory set aside for the string with new would also be leaked (although it is leaked anyway in that example). That is why I said a pointer to a string is passed by reference.

    Pass-by-reference is used to refer to passing by pointer or reference in C++, and means that the original value that you are passing can be updated by the function. So depending on whether you are referring to a string, a pointer to a string, or a pointer to a pointer to a string, either pass-by-value or pass-by-reference could be accurate here.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ok. Thanks. It's just that when I look at the argument passing, I look at the actual object passed. Not at the referent.

    The string is never passed. The address of the string is. And it is passed by value, according to the rules of how pointer parameters are initialized. I never thought of looking at it from the referent perspective.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The address of the string is. And it is passed by value.
    Well, technically it is the address of the pointer that points to the string that is passed by value, but I think that's what you meant.

    If you look at it that way, then there is no such thing as pass-by-reference in C or C++. However, the idea of passing by reference is an important tool in both languages so it would make more sense to allow either definition and just be clear about which you are referring to.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    No, no.
    I'm referring to the bit where you say "A pointer to a string is passed be reference."
    I'm not talking about pointers to pointers.

    Just to clarify, I didn't think discussing passing-by-reference and passing-by-value was meaningful in the context of the referent. I always thought that the context of those two expressions was the actual object being passed.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    This is pass by value:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    void SomeFunction(string** pointerToStringPointer)
    {
        **pointerToStringPointer = "Goodbye world!";
    }
    
    
    int main()
    {
        string   helloString                 = "Hello world!";
        string*  pointerToHelloString        = &helloString;
        string** pointerToHelloStringPointer = &pointerToHelloString;
    
        SomeFunction(pointerToHelloStringPointer);
        
        cout << helloString;
    };
    This is pass by reference to do the same thing:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    void SomeFunction(string*& referenceToStringPointer)
    {
        *referenceToStringPointer = "Goodbye world!";
    }
    
    
    int main()
    {
        string   helloString                 = "Hello world!";
        string*  pointerToHelloString        = &helloString;
        string** pointerToHelloStringPointer = &pointerToHelloString;
    
        SomeFunction(*pointerToHelloStringPointer);
        
        cout << helloString;
    };
    Your friends are right.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, that's what I always tought to.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> This is pass by value
    Nope, both are passing the string by reference. The output of both is "Goodbye world!". If you were passing the string by value, then the output would be "Hello world!".

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I always thought that the context of those two expressions was the actual object being passed.

    If I understand you correctly, you are saying that in the code below, whether or not pass-by-value or pass-by-reference is being used is based on how the pointer is passed.
    Code:
    void foo(int* pi)
    {
    }
    
    int main(void)
    {
      int i = 1;
      foo(&i);
    }
    I'm saying that you shouldn't limit it to that context, otherwise it would always be pass-by-value and we would have nothing interesting to talk about. Instead, you would normally refer to that code as an example of passing the int by reference. It uses a pointer as the mechanism for passing by reference, but it is still pass by reference.

    I don't know why the original example uses a double pointer, that just confuses the issue.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    >> I don't know why the original example uses a double pointer, that just confuses the issue.

    Definitely.

    You got my point correctly. And I do think I get yours now.

    I do really feel strongly that a pointer at best simulates a pass-by-reference. It's a mechanism to achieve some form closely resembling pass-by-reference. It is not pass-by-reference by any means.

    A function prototype with a pointer parameter cannot change its argument in any way. It is true that the pointer parameter can have the ultimate goal of allowing the change of the referent. But this has nothing to do with pass-by-reference. It's just a simulation. A pass-by-reference can only happen with the help of a reference parameter.

    I know that at least IBM calls pass-by-reference, the use of pointers. But this is just plain wrong. As many other authors suggest (Lippman, Scott Meyers and Stroustrup himself).
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> But this is just plain wrong.
    I think "wrong" is the wrong word there. It might be debatable whether that use of the term is valid, but it is still used widely in that way. It is also a complex issue, since the word reference in the term pass-by-reference could be referring to C++ references, or to the generic ability to pass a reference to a value.

    Note that the FAQ below is an example of someone using my version of the terminology. I think that this is a much more appropriate way of looking at things since it conveys the point of the issue instead of the semantics of how it is done.

    http://c-faq.com/ptrs/passbyref.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing back pointer pointed to string
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-21-2007, 07:55 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM