Assuming I have 2 array of different size i.e
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int *arr2 = new int[5];
I want to shallow copy some of them, Deep copy equivalent would be
int j =0;
if(!(i%2))
{
arr2[j]=arr[i];
j++;
}
Right now a print of arr2 will output : 0, 2, 4, 6 ,8
The reason I want to shallow copy is because I want arr2 to update with any changes to arr.
That is if I loop and square all the elements in arr
I want arr2 to output : 0, 4, 16, 36 ,64
These 2 arrays are part of the same class, one is my polygonal information, and the other part is data driven. arr is actually 4000+ elements in size, and arr2 is close to 3000. At the moment my algorithm works great with deep copy. but because I need to deep copy 3000 elements per update frame, i am wasting resources and was wondering if i could somehow do this via shallow copy so I don't have to have to update arr2 every frame. The way my code needs it to work, arr actually has repeated values of arr2. arr2 is a list of points that is animated. then the data is duplicated to arr which hold the positional data for vertices. this is because arr contains multiple bezier patches, some of them share one edge or more with another patch. but i want that to be ignored when animating else there are breaks in the surface.
It is important that the copy involves indices like
arr2[j]=arr[i];
because that is how my code is setup. And that the operation be low load.
std::array
-like class, whic hasoperator[]
which does the right thing of referencing an original. Sort of like model-view thing. – Cressarr2[j] = arr[i];
is deep copy, and then you said it's important that the shallow copy isarr2[j]=arr[i];
. That doesn't make sense. Is there a problem with doingint *arr2 = arr;
? – Anzacint *arr2 = arr;
is different from*arr2 = arr
. No data is lost, you can access all ofarr
througharr2
– Anzac