-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange behavior of prepend! with subarrays #16642
Comments
I don't know how easy it would be to completely fix this. Memory can be aliased in a lot of different ways. |
But now we have undefined behavior of |
I haven't spent (and ATM can't spend) any real time thinking about this, but it occurs that operations like |
Those operations already do throw errors on views, since they're not defined for them. The issue here is prepending to an array the elements of some view that is based on that array itself. |
should be able to write a band aid SubArray specialized method that checks if the parent array is the one being prepended to, and does a copy if so? |
I guess there are many more possibilities for hidden aliasing. An easy one: a=collect(1:10);
a[2:6]=sub(a,1:5);
all(a[1:6].==1) # gives true Similarly, other view-of-another-array types would be affected by the same problem. If it wasn't for the special treatment of EDIT: Not a case likely to occur in practice, but a=collect(1:10);
prepend!(a, reshape(reshape(sub(a, 1:4), 2, 2), 4)) nicely exposes the problem with a |
Ref: #50824 |
The following code produces unexpected result:
and first three entries of
x
are garbage in result. The behavior is inconsistent with the following code:The reason is that
prepend!
checks ifa===items
and otherwise assumes thatitems
is unchanged when arraya
is grown at the beginning.The text was updated successfully, but these errors were encountered: