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

  • Home
  • SEARCH
  • 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 8088787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:11:20+00:00 2026-06-05T19:11:20+00:00

I have this unsafe code that I need to make safe so it can

  • 0

I have this unsafe code that I need to make safe so it can execute. I know about the fixed block, but I don’t know how to implement it:

    private static unsafe void PrintHex(byte* data, uint len)
    {
        uint ctr;
        string sep;

        if (len > 64)
        {
            len = 64;
        }

        for (ctr = 0; ctr < len; ctr++)
        {
            if (((ctr & 7) == 0) && (ctr != 0))
            {
                sep = "\n";
            }
            else
            {
                sep = "";
            }
            Console.Error.WriteLine("{0}{1:X}", sep, data[ctr]);
        }

        Console.Error.WriteLine("\n\n");

    }
  • 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-06-05T19:11:22+00:00Added an answer on June 5, 2026 at 7:11 pm

    It’s the responsibility of the calling code to ‘fix’ or ‘pin’ the pointer before calling your method, as in this example from the MSDN page for fixed:

    class Point
    { 
        public int x, y; 
    }
    
    class FixedTest 
    {
        // Unsafe method: takes a pointer to an int.
        unsafe static void SquarePtrParam (int* p) 
        {
            *p *= *p;
        }
    
        unsafe static void Main() 
        {
            Point pt = new Point();
            pt.x = 5;
            pt.y = 6;
    
            // Pin pt in place:
            fixed (int* p = &pt.x) 
            {
                SquarePtrParam(p);
            }
    
            // pt now unpinned
            Console.WriteLine ("{0} {1}", pt.x, pt.y);
        }
    }
    

    The fixed statement sets a pointer to a managed variable and “pins”
    that variable during the execution of statement. Without fixed,
    pointers to movable managed variables would be of little use since
    garbage collection could relocate the variables unpredictably. The C#
    compiler only lets you assign a pointer to a managed variable in a
    fixed statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some 'legacy' code (which I can't change, but need to add on
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
Can any one help me. I have this ‘unmanaged’ .NET code, which works on
I have some unsafe C# code that does pointer arithmetic on large blocks of
I have some code which must be thread safe whose behavior resembles this: protected
I have code that looks like the following: public void GetData(dynamic dObj) { unsafe
I have this method that I need to call and use in my application,
I am about to send an html email in code that may contain unsafe
I need to write a test which verifies that my code can handle an
We have this software that has a webservices component. Now, the administrator of this

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.