简体   繁体   中英

Swapping pointer to pointer

In this program I'm writing, I'm using 2 matrices declared as pointer to pointer. Initially, matrix B is equal to matrix A, and all the changes are being made to matrix B (I need to not modify the values in A because I'm using those values to compute something else and if I directly modify it I practically get the wrong result). In the end, I need to swap the values in the two matrices. My program is already running and compiling, but to swap the matrices I've used

for(i=0;i<n;++i)
    for(j=0;j<n;++j)
        A[i][j]=B[i][j];

I know that's not the best method, so I was wondering whether there's a way to swap my matrices by pointers. I've already tried to do it myself. but I'm new to C++ programming and I cannot seem to manage to do it right :(.

This is a sketch of my code:

void swap(int **A, int **B){
?
}

main (){
int **A, **B;
*code*
swap(A,B);
}

C++ already gives us a swap function :

int main()
{
   int** A;
   int** B;

   /* ... code ... */

   std::swap(A, B);   
}

What it does in this particular case is basically this:

void swap(int**& lhs, int**& rhs)
{
   int** tmp;

   tmp = lhs;
   lhs = rhs;
   rhs = tmp;
}

Or, with pointers instead of references:

void swap(int*** lhs, int*** rhs)
{
   int** tmp;

   tmp = *lhs;
   *lhs = *rhs;
   *rhs = tmp;
}

With this last one, you'd call swap(&A, &B) (note the & ).

That said, why all these pointers? You're in C++. Use containers.

At the very least consider using real arrays, because I have serious doubts that the type int** is doing what you think it's doing.

It looks like this is a filtering or pipeline type operation to me so what I would suggest is simply swapping the pointers around rather than moving each element.

You will need an intermediate pointer to do the swapping but since it is just a pointer this is a fixed amount of storage compared the to size of your matrices so it should be much faster than copying if the data set is sufficiently large.

int **A,**B,**tmp;
tmp = A;
A=B;
B=tmp;

As mentioned above a struct or other container may be useful especially if you do any dynamic allocation of memory and need to free it to avoid memory leaks.

From the detailed description of your problem, it appears that "swap" doesn't really convey what you want - your for loop assigns the values in B to the values in A . You're evidently willing to discard the old values in A . The next question is: Are you willing to discard the storage of A ? If so, it's as simple as

// Don't forget to deallocate A first, as appropriate to however you allocated A.
A = B;

However, if you want to do your algorithm repeatedly you probably want to keep the storage around rather than thrashing free store. If you want the next iteration to start from the output of your previous iteration (ie you want A and B to both hold the matrix you just calculated) then your for loop is as fast as you can reasonably expect.

If you expect to start afresh with new data, then std::swap(A,B) is what you want. #include <algorithm> to get access to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM