CSharp fixed keyword

A03B0049Since I’ve already mentioned my bias against using unsafe mode in this post:

Advanced CSharp – unsafe mode

I’ll skip my normal rant about that.  Just suffice it to say they don’t call it “unsafe” for nothing.

So assuming you have some valid use for using unsafe mode, you’ll also need to make sure your memory isn’t moving around on you while you are accessing the pointers you’ve created in your unsafe block.  For that, you’ll need the fixed keyword.

While the unsafe mode gives you the ability to use pointers in the first place, fixed makes sure the pointers aren’t moving around while you are using them.  We need both so that we only fix the memory in its location while we are using it while still being able to write normal code within our unsafe block.

To create a pointer to a variable, you use the asterisk (*).  To retrieve the address of a variable (so you can assign it to the variable) you use the ampersand (&).  Just like we did in C++ for those of you who are familiar with that language.

So to create a pointer to an integer, your code might look something like this:

public unsafe void Foo()
{
    int i = 20;
    int* pi = &i;
}

At this point, pi points to i, which holds the value of 20.

But if we want to use pi to retrieve the value of 20, we want to make sure that the memory location of i doesn’t move while we are retrieving it.

Arguably, this is much more important when we are trying to access arrays in a loop.  But to keep the illustration simple we’ll continue with retrieving our single integer value.

public unsafe void Foo()
{
    int i = 20;
    int copyOfI;
    fixed (int* pi = &i)
    {
        copyOfI = *pi;
    }
}

Now we can be sure that every time we access pi, it will still be pointing to the variable i.

Related Post

  • Stackalloc in CSharpStackalloc in CSharp In the last few weeks we've looked at several keywords from the CSharp language that allow us to deal with memory management directly.  Stackalloc is another keyword from that list. Before we l...
  • Readonly variables in CSharp? Really?!Readonly variables in CSharp? Really?! I'm sure most of you are familiar with the keyword "const," which allows you to declare a variable and give it a value and assures that no other code will change the value. const int v = 23; p...
  • Dispose, Finalize and SuppressFinalizeDispose, Finalize and SuppressFinalize I got the following question recently. What is the difference between Dispose and SupressFinalize in garbage collection?” The problem with this question is it assumes Dispose and SupressFina...
  • Making values nullableMaking values nullable First, a little history lesson. When .NET was first released, we had value types and object types.  The difference between the two is that you do not have to set aside memory for a value type b...
  • Delegates in .NETDelegates in .NET I received the following question: What is a delegate?  What problem does it solve?  and When might I use a delegate? A delegate is essentially a function pointer.  We have used function poi...
  • Pingback: Dew Drop - January 6, 2009 | Alvin Ashcraft's Morning Dew

  • http://www.dbihosting.com/cpanel_vps.html cPanel VPS

    In which case would you not want to used the fixed keyword? Maybe I am a little slow today, but I can’t think of a scenario where you would want to point to something that is moving around in memory space?

  • Dave

    I’ve been thinking about this question since you posted it and I’ve come to the conclusion that you either don’t understand the context in which this was written or I don’t understand your question.

  • Jonathan

    cPanel VPS. I understand your confusion and let me add this explanation. You use fixed so garbage collection won’t clean up your variable. You always want to use fixed. It says while i am in this fixed code block please don’t clean up the information I am pointing to.

    If my statement is still not clear, here is the msdn article
    http://msdn.microsoft.com/en-us/library/f58wzh21.aspx

  • Dave

    To be clear on the “always want to use fixed.” You always want to use it when you are working with memory in unsafe mode. Normal, day-to-day usage of .NET, you’d never want to use it.