Passing a pointer by reference

16,491

Solution 1

An array decays to a pointer when you pass it as an argument to a function call but not to a reference to a pointer.

Imagine this:

void foo(int*& p)
{
   p = new int;
}

int main()
{
   int arr[6] = {0};

    foo(arr);
   // You don't want arr to be an invalid object here.
   // If the compiler allowed you to do that, that's what will happen.
   // Not only that, foo(arr); is equivalent to:
   // arr = new int;
   // which is not allowed in the language.
}

Solution 2

You really should NOT pass a reference, because you don't really want merge changing the value of the pointer. You just want to allow it to change the values stored in the array, which it can do without the reference (i.e., just from the pointer).

Share:
16,491
1110101001
Author by

1110101001

Updated on June 04, 2022

Comments

  • 1110101001
    1110101001 almost 2 years
    #include <iostream>
    
    using namespace std;
    
    void merge(int *& toMerge, int lo, int mid, int hi)
    {
        int merged[hi+1];
        int i = lo, j = mid+1;
        for (int k = lo; k <= hi; k++)
        {
            if (i > mid) {merged[k] = toMerge[j]; j++;}
            else if (j > hi) {merged[k] = toMerge[i]; i++;}
            else if (toMerge[j] < toMerge[i]) {merged[k] = toMerge[j]; j++;}
            else {merged[k] = toMerge[i]; i++;}
        }
        toMerge = merged;
    }
    
    int main(int argc, const char * argv[])
    {
    
        int x[8] = {1,7,4,6,2,7,3,2};
        merge(x, 0, 7, 3);
        return 0;
    }
    

    I am trying to pass a pointer by reference here such that the end result will be that the array x will be sorted. However, my compiler is throwing an error that there is no matching function call for the merge(x, 0, 7, 3).

    I am not sure as to why this is happening because the merge function requires a pointer, and x is a pointer to the first element in the array -- so it should work. Why doesn't it?

  • 1110101001
    1110101001 almost 10 years
    Except I need an auxiliary array for mergesort and copying the elements back takes addition O(n) time.
  • Dwayne Towell
    Dwayne Towell almost 10 years
    Huh? The array values are not copied when passing either a pointer, or a pointer to reference, or a reference to a pointer, or a reference.
  • David G
    David G almost 10 years
    No, merge() has a syntactically correct signature. T*& is a reference to a pointer, not a pointer to a reference.
  • 1110101001
    1110101001 almost 10 years
    I know -- if I were to just use a pointer and change the values stored in the array, I would have to update each value in the array with the sorted values, taking O(n) time.
  • 1110101001
    1110101001 almost 10 years
    How would you explicitly pass it as a reference?
  • Xavier Leclercq
    Xavier Leclercq almost 10 years
    Another issue is that int merged[hi+1]; has local scope and you try to return it via a pointer. That pointer will then point to deallocated memory.
  • 1110101001
    1110101001 almost 10 years
    @XavierLeclercq True -- how does C++'s sort() handle this?
  • Dwayne Towell
    Dwayne Towell almost 10 years
    @0x499602D2, well, silly me. Thanks. Not sure where my head was.
  • chris
    chris almost 10 years
    @user2612743, It sorts in place. And there's no sort that takes O(n) time in general, so something being O(n) isn't really anything to worry about.
  • R Sahu
    R Sahu almost 10 years
    You can't. You have to ask yourself whether you need it.