I have an array a
which is constantly being updated. Let\'s say a = [1,2,3,4,5]
. I need to make an exact duplicate copy of a
and call
I have a feeling that all of these "better ways to copy an array" are not really going to solve your problem.
You say
I tried a for loop like [...] but that doesn't seem to be working correctly?
Looking at that loop, there's no obvious reason for it not to work ... unless:
a
and b
arrays messed up (e.g. a
and b
refer to the same array), ora
array simultaneously.In either case, alternative ways of doing the copying won't solve the underlying problem.
The fix for the first scenario is obvious. For the second scenario you will have to figure out some way of synchronizing the threads. Atomic array classes don't help because they have no atomic copy constructors or clone methods, but synchronizing using a primitive mutex will do the trick.
(There are hints in your question that lead me to think that this is indeed thread related; e.g. your statement that a
is constantly changing.)