I'm using memmove
to shift std::string
elements to the right by one spot. The first spot of the destination is the only spot that gets messed up and filled with garbage. I am using memmove
instead of strcpy
because I am using it in a class that requires templates. It works fine when I have an array of ints though. Any idea on how to fix this for strings?
/**
* @brief Shifts elements in the array to the right
* @param newItem The insert position.
*/
template<class T>
void DynamicArray<T>::shiftElementsRight(uint newItem) {
uint diff = _size - newItem;
memmove(&(_array[newItem + 1]),
&(_array[newItem]), sizeof(_array[0]) * diff);
}
My Main
int main() {
DynamicArray<string> array(1);
string val1 = "val1";
array.add(val1);
string val2 = "val2";
array.add(val2);
// ... more additions ...
The last output:
Sizeof: 8
Value is: val1
Value is: val11val3, val21
����[val1, val2A����[val1, val2, val31val4, Aq�����[val1, val2, val3 val4, val5Q`r�#����[val1, val2, val3, val4, val5, val61val5, 1val6, Q"����[val1, val2, val3, al4, a
8s��p�hp��p�hq�(r�Xr�KC�KC�KC�1val7, a:����[val1, val2, val3, val4, val5, qF����[val1, val2, val3, val4, val5, val6]