Hi, I read that using ArrayCopy on the same array could cause data corruption when the initial source index is lower than the target index, due to the left-to-right copy.
I tried, but no data corruption was detected. Can I be sure this won't happen under any circumstances?
Here a simple script:
Thanks for help.
Here in the documentation, we are warned that "It is permitted to copy elements from an array to itself. But if the target and source areas overlap, you need to keep in mind that the iteration is done from left to right".
In this forum post same conclusion, and also gemini google answer statament same thing.
- www.mql5.com
Here in the documentation, we are warned that "It is permitted to copy elements from an array to itself. But if the target and source areas overlap, you need to keep in mind that the iteration is done from left to right".
It's not stated in this link to the book that there could be a data corruption. Just that the iteration is done from left to right.
In this forum post same conclusion, and also gemini google answer statament same thing.
This forum post gives wrong information (possibly something changed since 2023).
In summary, there is no data corruption using ArrayCopy in MQL5.
ArraySetAsSeries doesn't move anything in memory, it only flips how the index is mapped (the docs say the elements aren't physically reordered). So it doesn't change the overlap itself, just which direction your start/count point to. Easy to test with your same script, but note the flag only works on dynamic arrays, a static int arr[6] just returns false:
int a[]; ArrayResize(a, 6); for(int i = 0; i < 6; i++) a[i] = i; ArraySetAsSeries(a, true); ArrayCopy(a, a, 1, 0, WHOLE_ARRAY); ArrayPrint(a);
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, I read that using ArrayCopy on the same array could cause data corruption when the initial source index is lower than the target index, due to the left-to-right copy.
I tried, but no data corruption was detected. Can I be sure this won't happen under any circumstances?
Here a simple script:
Thanks for help.