Pointers to pointers question

This is a discussion on Pointers to pointers question within the C++ Programming forums, part of the General Programming Boards category; Hi, I would like to know if passing a pointer to a pointer is faster than passing a pointer by ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Pointers to pointers question

    Hi, I would like to know if passing a pointer to a pointer is faster than passing a pointer by value... To make it clearer, let's take a look at these prototypes:
    Code:
    void abc(int a);
    void abc(int& a);
    void abc(int*a);
    void abc(int** a);
    So I just want to know, between these differences, what would it change to pass an int** when you could just use an int*? In other words, we know that the first line will have to make a copy of the variable and thus needing some more space in the program. But with the 2nd or 3rd lines, we pass an address so it doesn't have to take more memory copying the initial value... But a pointer too takes memory in the computer, (I believe) and so we can place a pointer to this pointer, but does that changes a thing? (int* -> int**)

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Passing a pointer is still a pass by value. It behaves exactly like the first function. The only difference being that the variable copied will be a pointer to int, instead of an int.

    Only C++ has pass by reference. Which is what you did on your second function.

    There may be uses for pointer to pointers. One of the most common ones is if you have a pointer to a container of pointers. Regardless, passing a pointer to a pointer is one way of simulating a pass by reference of a.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    But about memory usage/speed, in this case there is no difference?

    And I got some other question: how do you pass an array of member-functions pointers by
    reference? I think I'd know how to tell it should become a pointer expression, but in the parameters
    I don't know how to write it:

    Code:
    void	(*Func_Ptrs[10])(void*);
    
    //would something like this work?
    void* Name(**??)(void*);
    Name = Func_Ptrs;
    Last edited by mikahell; 07-22-2006 at 11:39 AM.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Quote Originally Posted by mikahell
    But about memory usage/speed, in this case there is no difference?
    Memory usage or speed should really not be your concern when deciding what type of parameters you are going to provide your function with. What should be in your mind is what you want the function to do...

    . You want it to return an lvalue? Your function should return a reference or pointer.
    . You want one of the parameters to have its value changed and that change to stick to the variable after the function ends? You should accept a reference or pointer.
    ... etc ...

    Of course, some objects are better passed by reference or pointer, no matter what. You surely don't want to pass by value a container with 10,000 doubles, or a string with 10 pages of a book. But your concern is what you want your function to do. Reason and common sense will dictate the rest.


    void (*Func_Ptrs[10])(void*);
    //would something like this work?
    I don't know and I don't even care
    As long as I don't need pointers to functions to do my programming, I plan to stay away from them as much as I can.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    All arguments are passed by value in C++.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    No they are not.

    Maybe you mean in C
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  7. #7
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well, if you don't care, does anyone else care?

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Well, everyone should care if suddenly all arguments in C++ could only be passed by value.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I would like to know if passing a pointer to a pointer is faster than passing a pointer by value...
    Unlikely. Either way you're passing a pointer, so the difference in space and time costs should be negligable.

    >how do you pass an array of member-functions pointers by reference?
    Making sure to keep your sanity in check, you would do it like this:
    Code:
    class class_type {
    public:
      void m_foo();
    };
    
    typedef void (class_type::*mf)();
    
    void foo ( mf (&array)[10] )
    {
      // Blah blah
    }
    
    int main()
    {
      mf array[10];
    
      foo ( array );
    }
    Whatever you do, don't ask for the same code without the typedef. It's extremely nasty, and no reasonable programmer would be caught dead writing it.
    My best code is written with the delete key.

  10. #10
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Hmm, all these foos got me mixed up.... But I'll try to understand your code

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Arrow

    A reference holds the address of an object, but behaves syntactically like an object.
    It is often more efficient to pass references, rather than large objects, to functions. This allows the compiler to pass the address of the object (by value) while maintaining the syntax that would have been used to access the object.
    Last edited by siavoshkc; 07-22-2006 at 12:56 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 05:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21