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 8737197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:27:55+00:00 2026-06-13T10:27:55+00:00

I have an ordered struct which contains fields.. [StructLayout(LayoutKind.Explicit)] public unsafe struct RunBlock_t {

  • 0

I have an ordered struct which contains fields..

[StructLayout(LayoutKind.Explicit)]
public unsafe struct RunBlock_t {
    [System.Runtime.InteropServices.FieldOffset(0)]  public fixed byte raw[512];
}

If I declare this inside a function and want to use the pointer, it works fine..

{
  RunBlock_t r = new RunBlock_t();
  for (int i=0; i<512; i++) r.raw[i]=0;
}

But if I declare the variable outside the scope, it requires a fixed implementation

RunBlock_t r;
{
  r = new RunBlock_t();
  fixed (byte* ptr = r.raw) for (int i=0; i<510; i++) ptr[i]=0;
}

Why this difference in behavior?

— EDITED —–

Just want to state again that any other permutation does not work.

    unsafe void foo() {
        RunBlock_t r = new RunBlock_t();
        fixed (byte* ptr = r.raw) for (int i = 0; i < 512; i++) ptr[i] = 0;
    }

Generates You cannot use the fixed statement to take the address of an already fixed expression and does not compile.

    RunBlock_t r;
    unsafe void foo() {
      r = new RunBlock_t();
      for (int i=0; i<512; i++) r.raw[i]=0;
    }

Generates You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement. and does not compile.

  • 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-13T10:27:56+00:00Added an answer on June 13, 2026 at 10:27 am

    You have, unfortunately, confused the question slightly. If I copy from the question exactly, then this works fine:

        {
            RunBlock_t r = new RunBlock_t();
            for (int i = 0; i < 512; i++) r.raw[i] = 0;
        }
    

    and this:

        RunBlock_t r;
        {
            r = new RunBlock_t();
            fixed (byte* ptr = r.raw) for (int i = 0; i < 510; i++) ptr[i] = 0;
        }
    

    raises:

    You cannot use the fixed statement to take the address of an already fixed expression

    And if we remove the fixed, it works.

    What you should have shown was the function signature, i.e.

    RunBlock_t r;
    unsafe void Bar()
    {
        {
            r = new RunBlock_t();
            fixed (byte* ptr = r.raw) for (int i = 0; i < 510; i++) ptr[i] = 0;
        }
    }
    

    Now the meaning becomes clearer. You see, when you use fixed to access a fixed-buffer in a value, you aren’t actually fixing the buffer, nor are you fixing the value; what you are actually fixing is the containing object, i.e. the object that has the r field. This is to prevent GC from moving it around on the stack, which would be bad if we are accessing it as a pointer at the time. In our example above, our expression is really fixed (byte* ptr = this.r.raw), and the thing that gets pinned is: this.

    This is different if we only have a struct as a local. Locals are on the stack; they are (as the earlier message hinted) already fixed; the stack is never relocated by GC.

    So:

    • if you have a struct as a local variable, you do not need to use fixed – you are just accessing it as a pointer directly (via ldloca)
    • if you have a struct that is a field in an object, you need to use fixed, to pin the object in place for the duration of your manipulations
    • in cases where you’re passing a reference to the struct (i.e. a ref RunBlock_t parameter), then you must use fixed just in case it is a field on an object; if the reference turns out to resolve to the stack, then it doesn’t need to do anything
    • note that everything here about “field on an object” also applies equally to “value in an array”, i.e. if we are talking about someArray[8] (since you can manipulate the contents of arrays in-situ)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ordered list which I have styled in two ways: Coloured the
I have sever JPanels which have to be ordered vertically. For that I want
I have a class defined like this: public class StateMachineMetadata<T> where T: struct {
I have an ordered list which has a <input type=text... and <button> field. When
I currently have an ordered JSON string being passed into my iPhone app, which
I need to get a count of which types of tickets have been ordered
I have a struct Test, which I add as a value to a dictionary.
I have an ordered list in my Word document, and I want to add
I have an ordered string that I need to present to a user: ABCCDDCBBBCBBDDBCAAA
We have an ordered list <ol> <li>something good</li> <li>even better</li> <li>possibly the best</li> </ol>

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.