Here's a little something I stumbled upon by accident.
It might be user-error and not actually a bug, but it sure is weird behaviour in my book.
The following code defines an array, and fills it with 16 values. The values are not really important. Important is that there are 16, which is the default size a BaseArray
gets allocated (if I remember correctly).
The array is printed to the console.
Then a simple "shifting" of the array is performed. The amount of shifting isn't important.
And finally the shifted array is printed to the console ...
maxon::BaseArray<Int32> myArray;
for (Int32 i = 0; i < 16; ++i)
{
iferr(myArray.Append(i))
{}
}
ApplicationOutput("Before shifting: @", myArray);
const Int32 shift = 10;
for (Int32 i = 0; i < shift; ++i)
{
iferr(myArray.Append(myArray[0]))
{}
iferr(myArray.Erase(0))
{}
}
ApplicationOutput("After shifting: @", myArray);
... and here's the result of the above code:
Before shifting: {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
After shifting: {10,11,12,13,14,15,1835887981,1,2,3,4,5,6,7,8,9}
Where did value 0 go?
Weird, isn't it?
All we did was append the first value of the array at the end of that array.
Then erase the first value.
Repeat this 10 times.
Now, I know that the first time the MyArray.Append(myArray[0])
is performed the array needs to increment in size. All fine and well, but one would expect that element myArray[0] gets appended to the resized array, and not some uninitialized value.
Sure, one could use Resize
or EnsureCapacity
and increment the array upfront.
Or alternatively first copy the element, then erase it, and finally append it at the end. This would avoid the need for the array to grow. As in following:
const Int32 shift = 10;
for (Int32 i = 0; i < shift; ++i)
{
const Int32 tmp = myArray[0];
iferr(myArray.Erase(0))
{}
iferr(myArray.Append(tmp))
{}
}
But still ... who would have expected the result from the original code?
I sure didn't expect it.
So, is this a bug, or user-error?