Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1032695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:07:06+00:00 2026-05-16T14:07:06+00:00

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

  • 0

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

I need to iterate over them all, searching for certain patterns. The search is inherently reinterpret-cast style: at each offset, I need to consider a value as if it were a float, a double, a short, an int, etc. So getting a byte* for each input byte[] and incrementing it on each iteration seems like a natural approach.

Unfortunately I can’t find any way to create a collection of byte* – or, more specifically, to initialize it from a collection of arrays. Any ideas?

Here’s 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]++;
        }
    }
}

Essentially the question is how to initialize pointers with the addresses of arrays, while pinning the arrays so that GC doesn’t move them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:07:07+00:00Added an answer on May 16, 2026 at 2:07 pm

    use GCHandle.Alloc() from 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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty large db in MySql, and I need to take backups
All thread create methods like pthread_create() or CreateThread() in Windows expect the caller to
What is difference between unsafe code and unmanaged code in C#?
I have a web service that is willing to output gzip/deflated data. I've verified
I saw this question that asks given a string smith;rodgers;McCalne how can you produce
Is there anything wrong with the thread safety of this java code? Threads 1-10
Another possibly inane style question: How should concurrency be locked? Should the executor or
We know that int is a value type and so the following makes sense:
I was curious how the StringBuilder class is implemented internally, so I decided to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.