Right method of swapping arrays and classes using pointers
-
I have read a lot of stuf of programmers triying to swap arrays using for over every element like here (two pages): [^] There also other methods that involves making swapping element by element but it is hidden: std::swap(array1,array2); It would be better using pointers, with only 3 operations all the elements are swapped: double *a,*b; two arrays double **ptr1,**ptr2,**ptrx; ptr1=&a;ptr2=&b; Swapping: ptrx=ptr1;ptr1=ptr2;ptr2=ptrx; Example code:
#include
using namespace std;
double *a;
double *b;class c_class { public: double x; } ;
void main()
{
//1. Initialize of the array:
a = new double[7];
b = new double[7];
long i;
for (i = 0; i < 7; i++)
{
a[i] = i + 100;
b[i] = i + 300;
}//2. We need three pointers: double \*\*ptr1 = &a,\*\*ptr2=&b,\*\*ptrx; //3. Printing initial array values: cout << "=== SWAPPING ARRAYS ===" << endl; cout << "STEP 1: ptr1= " << endl; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; //4. Swapping the arrays in only 3 pointer operations: cout << " Swapping the arrays" << endl; ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx; //5. Printing the arrays after swapping: cout << "STEP 2: ptr1= "; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; //6. Swapping again the arrays in only 3 pointer operations: cout << " Swapping the arrays" << endl; ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx; //7. Printing the arrays after swapping: results must be the same than initial ones: cout << "STEP 3: ptr1= "; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; cout << "\\n\\n=== SWAPPING CLASSES ==="<x << " "<x<
-
I have read a lot of stuf of programmers triying to swap arrays using for over every element like here (two pages): [^] There also other methods that involves making swapping element by element but it is hidden: std::swap(array1,array2); It would be better using pointers, with only 3 operations all the elements are swapped: double *a,*b; two arrays double **ptr1,**ptr2,**ptrx; ptr1=&a;ptr2=&b; Swapping: ptrx=ptr1;ptr1=ptr2;ptr2=ptrx; Example code:
#include
using namespace std;
double *a;
double *b;class c_class { public: double x; } ;
void main()
{
//1. Initialize of the array:
a = new double[7];
b = new double[7];
long i;
for (i = 0; i < 7; i++)
{
a[i] = i + 100;
b[i] = i + 300;
}//2. We need three pointers: double \*\*ptr1 = &a,\*\*ptr2=&b,\*\*ptrx; //3. Printing initial array values: cout << "=== SWAPPING ARRAYS ===" << endl; cout << "STEP 1: ptr1= " << endl; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; //4. Swapping the arrays in only 3 pointer operations: cout << " Swapping the arrays" << endl; ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx; //5. Printing the arrays after swapping: cout << "STEP 2: ptr1= "; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; //6. Swapping again the arrays in only 3 pointer operations: cout << " Swapping the arrays" << endl; ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx; //7. Printing the arrays after swapping: results must be the same than initial ones: cout << "STEP 3: ptr1= "; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; cout << "\\n\\n=== SWAPPING CLASSES ==="<x << " "<x<
-
All you are doing there is changing the pointers, which makes no real sense. What problem are you trying to solve?
Given his code it will be a sort routine like bubble sort, but it looks like homework to me so I am reluctant. Usually they are asked to do an in place array sort and it. He hasn't worked out how to typecast the pointers properly and it seems to escape him that you can just malloc the arrays which is what his "new" statement hides.
In vino veritas
-
I have read a lot of stuf of programmers triying to swap arrays using for over every element like here (two pages): [^] There also other methods that involves making swapping element by element but it is hidden: std::swap(array1,array2); It would be better using pointers, with only 3 operations all the elements are swapped: double *a,*b; two arrays double **ptr1,**ptr2,**ptrx; ptr1=&a;ptr2=&b; Swapping: ptrx=ptr1;ptr1=ptr2;ptr2=ptrx; Example code:
#include
using namespace std;
double *a;
double *b;class c_class { public: double x; } ;
void main()
{
//1. Initialize of the array:
a = new double[7];
b = new double[7];
long i;
for (i = 0; i < 7; i++)
{
a[i] = i + 100;
b[i] = i + 300;
}//2. We need three pointers: double \*\*ptr1 = &a,\*\*ptr2=&b,\*\*ptrx; //3. Printing initial array values: cout << "=== SWAPPING ARRAYS ===" << endl; cout << "STEP 1: ptr1= " << endl; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; //4. Swapping the arrays in only 3 pointer operations: cout << " Swapping the arrays" << endl; ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx; //5. Printing the arrays after swapping: cout << "STEP 2: ptr1= "; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; //6. Swapping again the arrays in only 3 pointer operations: cout << " Swapping the arrays" << endl; ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx; //7. Printing the arrays after swapping: results must be the same than initial ones: cout << "STEP 3: ptr1= "; for (i = 0; i < 7; i++) cout << (\*ptr1)\[i\] << " "; cout << " ptr2= "; for (i = 0; i < 7; i++) cout << (\*ptr2)\[i\] << " "; cout << endl; cout << "\\n\\n=== SWAPPING CLASSES ==="<x << " "<x<
As I am sure this is homework I won't give you the answer, what I will tell you is the problem with the typecast Using the dereference operator (the *) is like mathematics there is an order to things just like in mathematics. If I gave you 2 + 3 * 4 and you wanted the add before the multiply you need brackets (2 + 3) * 4 the mult carries higher precedence normally. Same problem happens in dereference operator when using arrays double **a; *a[2] has two possible breakdowns so lets use brackets (*a)[2] or *(a[2]) *a[2] is actually equivalent to the latter *(a[2]) I bet that is not what you were expecting and reading it as :-) Your code doesn't do anything like what your text comments say because you are missing brackets. You need the dereference completed prior to the array use and (*a)[2] is the correct use for you. Lets give you a simple code
double myArray\[6\] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 }; double \*p = &myArray\[0\]; // P points at my array double d = p\[3\]; // I can get an array value from a double\* it will be 3.0 double \*\*q = &p; // Q points to p double s = (\*q)\[3\]; // This will correctly return 3.0 .. \*q evaluates first double t = \*q\[2\]; // This will crash and burn it thinks q is an array of double\* and wants \*item\[2\]
Does that last line look familiar? It is the same if the you are derefencing an object or a class *a.xyz may naively be intended as (*a).xyz or *(a.xyz) *a->xyz may naively be intended as (*a)->xyz or *(a->xyz) So look up and learn the precedence order of dereferencing and if you want the other USE BRACKETS. It's actually good practice to always use brackets if you are dereferencing complex items so you know for sure what the order will be be, you do the same with long chains with mathematics. It's a normal gotcha to not dereference what you think you are on complex items because of dereference precedence order (you can't just read it left to right and expect that order).
In vino veritas
-
As I am sure this is homework I won't give you the answer, what I will tell you is the problem with the typecast Using the dereference operator (the *) is like mathematics there is an order to things just like in mathematics. If I gave you 2 + 3 * 4 and you wanted the add before the multiply you need brackets (2 + 3) * 4 the mult carries higher precedence normally. Same problem happens in dereference operator when using arrays double **a; *a[2] has two possible breakdowns so lets use brackets (*a)[2] or *(a[2]) *a[2] is actually equivalent to the latter *(a[2]) I bet that is not what you were expecting and reading it as :-) Your code doesn't do anything like what your text comments say because you are missing brackets. You need the dereference completed prior to the array use and (*a)[2] is the correct use for you. Lets give you a simple code
double myArray\[6\] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 }; double \*p = &myArray\[0\]; // P points at my array double d = p\[3\]; // I can get an array value from a double\* it will be 3.0 double \*\*q = &p; // Q points to p double s = (\*q)\[3\]; // This will correctly return 3.0 .. \*q evaluates first double t = \*q\[2\]; // This will crash and burn it thinks q is an array of double\* and wants \*item\[2\]
Does that last line look familiar? It is the same if the you are derefencing an object or a class *a.xyz may naively be intended as (*a).xyz or *(a.xyz) *a->xyz may naively be intended as (*a)->xyz or *(a->xyz) So look up and learn the precedence order of dereferencing and if you want the other USE BRACKETS. It's actually good practice to always use brackets if you are dereferencing complex items so you know for sure what the order will be be, you do the same with long chains with mathematics. It's a normal gotcha to not dereference what you think you are on complex items because of dereference precedence order (you can't just read it left to right and expect that order).
In vino veritas
:thumbsup:
-
All you are doing there is changing the pointers, which makes no real sense. What problem are you trying to solve?
The idea is use the pointers to avoid copying all the data. An example, if there are 3 arrays of pixels: A, B, C and I want to order the arrays I can make the following operation:
if (distance(A,B)>distance(A,C))
swap(B,C);I tried this code that swaps anything but it does not compile:
#include
#includeusing namespace std;
void swap(void *&a,void *&b)
{
void *x=a;a=b;b=x;
}void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
cout << "a:"< -
The idea is use the pointers to avoid copying all the data. An example, if there are 3 arrays of pixels: A, B, C and I want to order the arrays I can make the following operation:
if (distance(A,B)>distance(A,C))
swap(B,C);I tried this code that swaps anything but it does not compile:
#include
#includeusing namespace std;
void swap(void *&a,void *&b)
{
void *x=a;a=b;b=x;
}void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
cout << "a:"<Why are you using
void*
in your swap method and calls? Void pointers do not point to anything so the compiler cannot figure out what to do. Since both arrays are ofint
type, then that is the type of pointer you should be using. Also your swap method does not make much sense, again you are swapping pointers not data. -
Why are you using
void*
in your swap method and calls? Void pointers do not point to anything so the compiler cannot figure out what to do. Since both arrays are ofint
type, then that is the type of pointer you should be using. Also your swap method does not make much sense, again you are swapping pointers not data.Because I would like to swap pointers, not to copy all the data. If I swap 2 arrays of 1k size using pointers is 1000x times faster than copying all the data. This code do it using void * pointers (but I do not know if it could work in other SO), so the next step is to place it in a function:
int a\[3\]={100,101,102}; int b\[3\]={200,201,202}; void \*x;int \*a1=a,\*b1=b; x=(void \*) a1;a1=b1;b1=(int \*) x; cout << "a:"<
I tried this function using C++11 and seems to work:
template
void swap(A *&a,A *&b)
{
A *x=a;a=b;b=x;
}void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
int *a1=a,*b1=b;
swap(a1,b1);
cout << "a:"<Unfortunately I cannot call swap using a and b:
swap(a,b)
-
Because I would like to swap pointers, not to copy all the data. If I swap 2 arrays of 1k size using pointers is 1000x times faster than copying all the data. This code do it using void * pointers (but I do not know if it could work in other SO), so the next step is to place it in a function:
int a\[3\]={100,101,102}; int b\[3\]={200,201,202}; void \*x;int \*a1=a,\*b1=b; x=(void \*) a1;a1=b1;b1=(int \*) x; cout << "a:"<
I tried this function using C++11 and seems to work:
template
void swap(A *&a,A *&b)
{
A *x=a;a=b;b=x;
}void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
int *a1=a,*b1=b;
swap(a1,b1);
cout << "a:"<Unfortunately I cannot call swap using a and b:
swap(a,b)