I am working with image and video manipulation and in a few cases really do need unsafe code for speed purposes. Sometimes I use code something like the following:
var data = new byte[n];
fixed (byte* fixData = data)
{
byte* ptrData = &fixData[0];
for (int i = 0; i < n; i++)
{
*(ptrData++) = 42;
}
}
My question is, since ptrData
is incremented n
times, it ends up pointing outside the array, but is this a problem? It is never assigned anything of course, since the increment happens after the last value-assignment, but could this code still break something, and if so, how serious? I have run code like this 1_000s of times and never encountered a problem, but still?