pointer arithmetic and std::vector
Hello all,
I have the following problem. Let's say you have three std::vector
objects and you need to access the contiguous memory as if they were
part of a regular array. That is, I would like to access each vector
using pointer arithmetic such that the beginning of each vector is
obtained by an offset or displacement from the first vector.
The following code works in my computer with GNU g++ v4.3:
#include <vector>
#include <iostream>
using namespace std;
int main() {
typedef std::vector<int> vec;
vec v1(10,1);
double* temp = new double(22.3);
vec v2(20,2);
vec v3(30,3);
cout<<"printing vector 1"<<endl;
for (int i=0; i<v1.size(); ++i)
cout<<" "<<v1[i];
cout<<endl;
cout<<"printing vector 2"<<endl;
for (int i=0; i<v2.size(); ++i)
cout<<" "<<v2[i];
cout<<endl;
cout<<"printing vector 3"<<endl;
for (int i=0; i<v3.size(); ++i)
cout<<" "<<v3[i];
cout<<endl;
int* add1 = &v1[0];
int* add2 = &v2[0];
int* add3 = &v3[0];
cout<<"printing vector 1 using address"<<endl;
for (int* addc1 = add1; addc1 != add1 + v1.size();)
cout<<" "<<*addc1++;
cout<<endl;
cout<<"printing vector 2 using address"<<endl;
for (int* addc2 = add2; addc2 != add2 + v2.size();)
cout<<" "<<*addc2++;
cout<<endl;
cout<<"printing vector 3 using address"<<endl;
for (int* addc3 = add3; addc3 != add3 + v3.size();)
cout<<" "<<*addc3++;
cout<<endl;
cout<<"printing address 1 -> "<<add1<<endl;
cout<<"printing address 2 -> "<<add2<<endl;
cout<<"printing address 3 -> "<<add3<<endl;
cout<<"address diference add2-add1 -> "<<add2-add1<<endl;
cout<<"address diference add3-add1 -> "<<add3-add1<<endl;
int* v2new = add1;
v2new += (add2-add1);
cout<<"printing vector 2 using new pointer"<<endl;
for (; v2new != add2 + v2.size();)
cout<<" "<<*v2new++;
cout<<endl;
int* v3new = add1;
v3new += (add3-add1);
cout<<"printing vector 3 using new pointer"<<endl;
for (; v3new != add3 + v3.size();)
cout<<" "<<*v3new++;
cout<<endl;
return 0;
}
The code prints:
aaragon@~/Desktop/test$./a.out
printing vector 1
1 1 1 1 1 1 1 1 1 1
printing vector 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
printing vector 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
printing vector 1 using address
1 1 1 1 1 1 1 1 1 1
printing vector 2 using address
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
printing vector 3 using address
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
printing address 1 -> 0x100150
printing address 2 -> 0x100190
printing address 3 -> 0x1001e0
address diference add2-add1 -> 16
address diference add3-add1 -> 36
printing vector 2 using new pointer
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
printing vector 3 using new pointer
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
So it seems that it works, but the question is if this ALWAYS will
work, independent of the value stored in the vectors or it will break
at some point. I found that using pointer arithmetic, the subtraction
is of type ptrdiff_t, and according to the cplusplus.com:
"A subtraction of two pointers is only granted to have a valid defined
value for pointers to elements of the same array (or for the element
just past the last in the array). For other values, the behavior
depends on the system characteristics and compiler implementation."
Thanks for the help.
aa