Initialize a set of bytes * in C #

My unsafe method accepts a collection byte[]s. All of these byte[]are the same size.

I need to iterate over all of them, looking for specific patterns. The search essentially has a reinterpret casting style: for each offset, I need to consider the value as if it were float, double, short, int, etc. Therefore, we obtain byte*for each input byte[]and its increment at each iteration seems natural.

Unfortunately, I cannot find a way to create a collection byte*- or, more specifically, initialize it from a set of arrays. Any ideas?

Here is a somewhat contrived version of the task:

static unsafe void SearchIteration(List<byte[]> arrays, byte[] resultArr)
{
    fixed (byte* resultFix = resultArr)
    {
        byte* resultPtr = resultFix;
        byte*[] pointers = new byte*[arrays.Count];

        <some code to fix all the arrays and store the pointers in "pointers">

        int remaining = resultArr.Length;
        while (remaining > 0)
        {
            <look at resultPtr and each of the pointers and update *resultPtr>

            remaining--;
            for (int i = 0; i < pointers.Length; i++)
                pointers[i]++;
        }
    }
}

, , pointers arrays, , GC .

+3
1

GCHandle.Alloc() System.Runtime.InteropServices:

var handles = new GCHandle[arrays.Count];
byte*[] pointers = new byte*[arrays.Count];
for(int i = 0; i < arrays.Count; ++i)
{
    handles[i] = GCHandle.Alloc(arrays[i], GCHandleType.Pinned);
    pointers[i] = (byte*)handles[i].AddrOfPinnedObject();
}
try
{
    /* process pointers */
}
finally
{
    for(int i = 0; i < arrays.Count; ++i)
    {
        handles[i].Free();
    }
}
+2

Source: https://habr.com/ru/post/1761396/


All Articles