.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

CSharp fixed keyword

January 05, 2009 By: Dave

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.

 

Other post in Advanced CSharp
  • Two Interfaces. Same Method. Two meanings. - September 29th, 2008
  • Making values nullable - October 9th, 2008
  • CSharp's Property Shortcuts - October 23rd, 2008
  • Readonly variables in CSharp? Really?! - October 29th, 2008
  • Dispose with Using - November 10th, 2008
  • Delegates in .NET - December 4th, 2008
  • Using Sealed in CSharp - December 8th, 2008
  • CSharp checked and unchecked - December 11th, 2008
  • Advanced CSharp - unsafe mode - December 15th, 2008
  • Volatile variables and CSharp threads - December 22nd, 2008
  • What is the global keyword in CSharp? - December 29th, 2008
  • CSharp fixed keyword - January 5th, 2009
  • using - There's more there than you are using - February 2nd, 2009
  • Stackalloc in CSharp - February 16th, 2009
  • Removing Warnings from CSharp Compile Cycle - March 10th, 2009
  • && vs & and | vs ||... What's the difference? - March 16th, 2009
  • Advanced CSharp - yield - March 25th, 2009
  • Just say “No!” to C# Regions? Really?! - April 16th, 2009
  • C# “” better than string.Empty? - April 20th, 2009
  • .Net String Pool – Not Just For The Compiler - April 22nd, 2009
  • CSharp ?? Operator - May 18th, 2009
  • Using VB.NET From CSharp - July 1st, 2009

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • Using VB.NET From CSharp
  • iTextSharp – Adding Images
  • Hungarian Notation – Use What Works, Spit Out The Bones
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: csharp, fixed

5 Responses to “ CSharp fixed keyword ”

  1. # 1 Dew Drop - January 6, 2009 | Alvin Ashcraft's Morning Dew Says:
    January 6th, 2009 at 9:28 am

    [...] CSharp fixed Keyword (Dave M. Bush) [...]

  2. # 2 cPanel VPS Says:
    January 8th, 2009 at 12:00 pm

    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?

  3. # 3 Dave Says:
    January 8th, 2009 at 5:42 pm

    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.

  4. # 4 Jonathan Says:
    April 20th, 2009 at 12:38 pm

    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

  5. # 5 Dave Says:
    April 20th, 2009 at 1:29 pm

    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.

← Installing Silverlight2 Tools for Visual Studio
jQuery – Positioning Elements →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    January 2009
    S M T W T F S
    « Dec   Feb »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.