I never needed to use unsafe in the past, but now I need it to work with a pointer manipulating a bitmap.
I couldn't find any documentation that indicates otherwise, but I would like to understand better how unsafe works and if it makes any difference to use it inside or outside a loop.
Is it better to do:
unsafe
{
for (int x = 0; x < maxX; x++)
{
for (int y = 0; y < maxY; y++)
{
//Unsafe pointer operations here.
}
}
}
Or to do?:
for (int x = 0; x < maxX; x++)
{
for (int y = 0; y < maxY; y++)
{
unsafe
{
//Unsafe pointer operations here.
}
}
}
unsafe
and you would want to minimize theunsafe
area. Unless, there is some sort of an overhead to calling it in a loop. – Disoperationunsafe
footprint of the code. Why include more than you need to in the unsafe block? It just creates additional lines of code where you can make mistakes outside the norm. – Layardunsafe
. – Strauss