Hi guys, so basically this is what I tried to do:
I initialized an array on the heap, then made a pointer (p1) pointing to it, then output all the values of the array that p1 pointed to. (that part works fine)
Then I made a new array (second) the same size as the first, then initialized a pointer that pointed to it, then tried to copy them by iterating through all the values of the first pointer, but unfortunately it just spits out 0's for the second array instead of the first array. Why is this, and how should I be copying these arrays with pointers? (I'm obviously doing it wrong)
If I have used incorrect terminology or syntax then tell me, I'm struggling when it comes to conceptualizing pointers and references.
You forgot to have p1 point back to the beginning. In something like this, I wouldn't be modifying the pointer. You can treat the pointer like an array: p1[I].
Random Note:
You forgot to call delete to free resources.
Edit:
You also have not copied anything. Your for loop continuously updates p2 and p1 until they are both way out of bounds, but the array at second is left untouched.
Alright so I think I understood what you meant, but I'm having segmentation faults with my output inside the for loop and it's 0s when I move it outside.
#include <iostream>
usingnamespace std;
int main() {
int* first = newint[10];
int n[10]={1,2,4,8,16,32,64,128,256,512};
first = n; //Memory leak. No pointer points to the array you allocated with "new int[10]"
int* p1 = first;
for(int i=0; i<10; ++i) {
//You can replace the two lines below with a simple p[I]
cout << *p1 << endl;
++p1;
}
//By this point, p1 still points to the last element in the array n
int* second = newint[10];
int* p2; //Forgot to initialize this pointer; current points to random lcoation
for(int i=0; i<10; ++i) {
p2[i] = p1[i]; //Segfault. Who knows where p2 is pointing to
}
second = p2; //This line should not exist. Causes second memory leak
for(int i=0; i<10; ++i) cout << second[i] << " ";
cout << endl;
//No freeing of resources
}
Memory Space
Stack (Where program is located):
n -> {1,2,4,8,16,32,64,128,256,512}
Heap (Random memory you can allocate):
first -> int[10]
second -> int[10]
After your program runs (and crashes):
Stack (Where program is located):
first -> n -> {1,2,4,8,16,32,64,128,256,512} <- p1 (last value)
Heap (Random memory you can allocate):
int[10]
int[10]
(Orphaned memory)
Some Other Memory:
second -> p2 -> ???
How to copy one array to another with the same size:
1 2 3
int arr1[3] = {1, 2, 3}, arr2[3];
for(int I(0); I < 3; ++I)
arr2[I] = arr1[I];
In fact, you don't really need p1 nor p2 in your program.
In your case, call delete[].
1 2 3
delete[] first;
delete[] second;
//Make sure they still point to their respective arrays
Thanks for your help, I got it to work, but I had to change my first array to be allocated on the stack, because I couldn't find a way to assign the values using newint[10]{...}.
I think the problem was me incrementing the pointer in the first for loop, and it caused it to go out of bounds in the second. (Maybe)
Also, I wouldn't use p1 or p2 either, but sadly it's required to use pointers to change the values in this program.
Anyway here's my final code, for anyone else's benefit. I commented out delete[] first because I changed that in the beginning and it's not on the free store.
Oh I must have massively misunderstood then, so basically this whole time I've had pointers pointing to pointers?
No wonder you thought I was silly.. That clears a few things up..
Thanks again!
Here's another one.
Pointers hold a memory address, so when you write p1 = first, the memory address first holds is copied to p1. So they point to the same location, but not to each other.
There are two problems with this:
1) After declaration, arr[I] will be accessing the array at that index. So arr[3] is out of bounds.
2) The second line attempts to assign an entire list to an int, so it is like writing int I = {1, 2, 3};.
When you allocate memory for an array, you cannot pass any initial values. Everything becomes the type's default value.
Unfortunately, this means you'd have to use a for loop to assign initial values.