C++ pointer to different array
Assume I have an array a
and an array b.
Both have the same type and size but different values开发者_Python百科.
Now I create 3 or so pointers that point to different elements in a, you could say a[0]
, a[4]
and a[13]
.
Now if I overwrite a
with b
via a=b
- Where will the pointers point?
Do the pointers still point to their original positions in a
but the values they point to are now those of b
?
Arrays are not assignable in C++. Once you declare an array:
int a[10];
there is no way of overwriting it, only of changing the values it contains. Specifically, you can't do:
int a[10], b[10];
a = b; // cannot work in C++ (or C)
If you create the array dynamically and assign pointers:
int * a = new int[10];
int * p = a + 1;
int * b = new int[10];
a = b;
then the pointer p still points into the first array, but you have a memory leak.
In the case of a struct containing an array:
struct S {
int a[10];
};
S s1, s2;
int * p = s1.a + 1;
s1 = s2;
then the pointer p still points into the first struct's array, but the array contents will have been overwritten with the array contents from the second struct.
That's an interesting question. So lets break it down:
- Where will the pointers point?
Same place as they did before. They only contain the address of the memory, nothing more. Changing "a" will not change the pointers.
- Will the pointers point to their original positions in a but the values they point to are now those of b?
If a was created as
int *a = new int[34];
then no.
If you don't directly change the variables that are storing your pointers, nothing is going to change the location they point to. Changing the values stored in the "pointed to" locations will change the dereferenced values of your pointers, but they still point to the same place.
Learning how to use a good graphical debugger and stepping through a test program would help illustrate what is going on. I don't know if you are on the windows platform, but the visual studio (and I would think Visual C++ Express) debugger will show you everything you need to know so you can run your own experiment and see exactly what your code is doing.
You can not overwrite anything with a=b
. Effectively this will leak memory, by leaving your whole 'a' array somewhere into the memory with no pointer to it. 'a' will point to the first element of 'b' after a=b
.
After a declaration like int * a = new a[5];
, your 'a' point to the first element of the array. You can do pointer arithmetic like a++, which will then go to the second element in the array, leaving your first element with no pointer to it. The same way a=b will point it to the first element of the b array.
Your other pointers a[3], a[14] e.t.c. will still point to the same memory as before. Note that a[0] is the same as 'a'.
You must make a difference between arrays and pointers. If you use arrays you cannot make a=b
.
If you use pointers you can make a = b and that will mean that a points where b points. The values inside them will not change, but it will be impossible to access. Once you make a = b, when accessing a[3] you will access b[3], because a[3] means: "where a points + 3", and a points where b points, hence b[3].
If you don't free where a
was allocated, that info is still in memory, so if you made 3 pointers that point where a used to point + some value, that info is still accessible and not modified.
a=b won't work, but you can copy array b into a using STL. As an example :
int a[15] = {0};
int b[15] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int *p1 = a; // pointer to a[0]
int *p2 = a+4; // pointer to a[4]
int *p3 = a+13; // pointer to a[13]
std::copy(&b[0], &b[15], a);
If you have pointers to array a elements declared before the copy :
- pointers' address won't change if you copy b into a
- only pointers' values change
精彩评论