I'm messing around with arrays and attempting to find efficient ways of performing operations on the data.
Adding values at the end is easy enough but shifting the data on or more indexes back has drawbacks. I could potentially create an new array with sufficient space and copy the data starting at the shifted index and then delete the original and swap the pointers but I believe this method is memory inefficient.
I ended up stumbling upon utilizing memcpy() on the array itself assuming there's space in it for the shift and it worked beautifully, however I am concerned that it's not proper or if I'm doing the same thing only that not consciously.
My code looks like this
const int size = 25, buffer = 25;
int num[size+buffer]{};
initialize(num, size);
cout << "Values:" << endl;
print(num, size+buffer);
cout << endl << endl;
memcpy( &(num[size]), num, sizeof(int) * size );
cout << "Values:" << endl;
print(num, size + buffer);
cout << endl << endl;
initialize is a function to assign values from a file and its a simple for loop; By the second print function the array holds two copies of the values. If everything works as I think it is I'm thinking of shifting it the amount needed for the new values and then inserting them there. Seems clean and quick and hopefully memory efficient.
I would appreciate any feedback.