Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C#
Article

Unsafe programming in C#

Rate me:
Please Sign up or sign in to vote.
4.29/5 (43 votes)
29 May 20025 min read 206K   83   20
Discusses the concept of using pointers in C#

Introduction

There's always one particular topic that catches the fancy of most C/C++ programmers, and is considered quite complicated and difficult to understand:   pointers!

Albeit, whenever C# is discussed, most of the people I have come across are of the opinion (and pretty strong one, if I may add) that C# carries no concept of pointers. In fact, it's done away with it. However, unsafe code is that part of C# programming, which is all about programming with pointers. Unlike it's literal meaning, there is nothing unsafe about programming with pointers.

The reason it is so referred to as is because, unlike the conventional .NET development that is done, unsafe programming requires certain assumptions on the part of the programmer. In this article, I shall start off by differentiating two highly confused terms, unsafe code and unmanaged code. This will be followed by discussion of how to write unsafe code, that is, how to use pointers in C#.

Unsafe or unmanaged? That is the question

Managed code is that code which executes under the supervision of the CLR. The CLR is responsible for various housekeeping tasks, like:

  • managing memory for the objects
  • performing type verification
  • doing garbage collection

just to name a few. The user is totally isolated from the how's of the above mentioned tasks. The user doesn't get to manipulate the memory directly, because that is done by the CLR.

On the other hand, unmanaged code is that code which executes outside the context of the CLR. The best example of this is our traditional Win32 DLLs like kernel32.dll, user32.dll, and the COM components installed on our system. How they allocate memory for their usage, how memory is released, how (if any) type verification takes places are some of the tasks that are undertaken by them on their own. A typical C++ program which allocates memory to a character pointer is another example of unmanaged code because you as the programmer are responsible for:

  • calling the memory allocation function
  • making sure that the casting is done right
  • making sure that the memory is released when the work is done

If you notice, all this housekeeping is done by the CLR, as explained above, relieving the programmer of the burden.

Unsafe code is a kind of cross between the managed and unmanaged codes

It executes under the supervision of the CLR, just like the managed code, but lets you address the memory directly, through the use of pointers, as is done in unmanaged code. Thus, you get the best of both worlds. You might be writing a .NET application that uses the functionality in a legacy Win32 DLL, whose exported functions require the use of pointers. That's where unsafe code comes to your rescue.

Now that we have gone through the distinctions, let's get coding... unarguably the best part, what do you think?

Inside unsafe code

Writing unsafe code requires the use of two special keywords: unsafe and fixed. If we recall, there are three kinds of pointer operators:

  • *
  • &
  • ->

Any statement, or a block of code, or a function that uses any of the above pointer operators is marked as unsafe through the use of the unsafe keyword, as shown below:

C#
public unsafe void Triple(int *pInt)
{
  *pInt=(*pInt)*3;
}

All the above function does is triple the value passed to it. But notice that the address of the variable, containing the value to be tripled, is passed to the function. The function then does its work. Since the function is using the "*" pointer operator, the function is marked as unsafe, since the memory is being directly manipulated.

However, there is a problem. If you recall from the discussion above, unsafe code is managed code, and hence, is being executed under the CLR's supervision. Now, the CLR is free to move the objects in memory. One plausible reason could be to reduce the memory fragmentation. But in doing so, unknowingly and transparently to the programmer, the variable being pointed to could be get relocated to some other memory locations.

So, if *pInt pointed to a variable which was at address 1001, and the CLR performs some memory relocation to reduce fragmentation, the variable which was earlier located at 1001 could, after relocation, be stored at memory location 2003. This is a catastrophe, since the pointer becomes invalid as there is nothing at memory location 1001 after relocation! Probably that's one of the reason usage of pointers has been made to keep a low profile in .NET. What do you think?

Fixing the pointers

Enter the fixed keyword. When used for a block of statements, it tells the CLR that the object in question cannot be relocated, and thus, it ends up pinning the object. Thus, when pointers are used in C#, the fixed keyword is used pretty often to prevent invalid pointers at runtime. Lets have a look at how it works:

C#
using System;
class CData
{
    public int x;
}

class CProgram
{
    unsafe static void SetVal(int *pInt)
    {
        *pInt=1979;
    }
    
    public unsafe static void Main()
    {
        CData d = new CData();
        
        Console.WriteLine("Previous value: {0}", d.x);
        
        fixed(int *p=&d.x)
        {
            SetVal(p);
        }
        
        Console.WriteLine("New value: {0}", d.x);
    }
}

All we do here is assign the address of field x of class CData to integer pointer p, within the fixed block. Now, while statements within the fixed block are executing, the pointer shall continue to point to the same memory location because the CLR has been instructed to pin the variable until the fixed block execution finishes. Once the fixed block is done, the object can be relocated in memory by the CLR.

That's all there is to programming using pointers in C#. Just make sure that the block is unsafe and that the object being pointed to is fixed. And you are ready to leverage your knowledge of pointers in C# too!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I hold Early Acheiver in MCSE 2000, MCSE NT 4.0, MCP+I, and actively involved in programming using C/C++, .NET framework, C#, Win32 API, VB, ASP and MFC.

I also have various publications to my credit at MSDN Online Peer Journal, Windows Developer Journal (http://www.wdj.com/), Developer 2.0 (http://www.developer2.com/), and PC Quest (http://www.pcquest.com/).

Comments and Discussions

 
QuestionImage processing: C# or C++ Pin
VHGN17-Apr-12 23:02
VHGN17-Apr-12 23:02 
QuestionHow to implement direct access memory in c# Pin
Milad.D20-May-11 21:49
Milad.D20-May-11 21:49 
GeneralTake a look at this pointer tutorial Pin
Theo Bebekis14-Jul-09 2:47
Theo Bebekis14-Jul-09 2:47 
GeneralMy vote of 1 Pin
Theo Bebekis14-Jul-09 2:39
Theo Bebekis14-Jul-09 2:39 
QuestionHow to get a pointer to an int array [modified] Pin
Georgi Petrov25-Aug-06 2:30
Georgi Petrov25-Aug-06 2:30 
GeneralDisplaying Pointer value Pin
Soulblazer30-Jun-05 21:33
Soulblazer30-Jun-05 21:33 
GeneralGetting the pointer value Pin
Anonymous30-Jun-05 21:30
Anonymous30-Jun-05 21:30 
GeneralDllImport Or MC++ wrapper Pin
4space14-Nov-02 1:15
4space14-Nov-02 1:15 
QuestionThe uses of unsafe code? Pin
Alex Korchemniy6-Aug-02 6:58
Alex Korchemniy6-Aug-02 6:58 
AnswerRe: The uses of unsafe code? Pin
Code Boy7-Oct-02 19:22
Code Boy7-Oct-02 19:22 
GeneralRe: The uses of unsafe code? Pin
Anonymous9-Oct-02 3:49
Anonymous9-Oct-02 3:49 
GeneralRe: The uses of unsafe code? Pin
The Anomaly5-Dec-03 8:04
The Anomaly5-Dec-03 8:04 
GeneralRe: The uses of unsafe code? Pin
The Anomaly5-Dec-03 8:06
The Anomaly5-Dec-03 8:06 
AnswerRe: The uses of unsafe code? Pin
Anonymous20-Dec-02 2:36
Anonymous20-Dec-02 2:36 
GeneralRe: The uses of unsafe code? Pin
Anonymous20-Dec-02 2:38
Anonymous20-Dec-02 2:38 
GeneralRe: The uses of unsafe code? Pin
Stephane Rodriguez.2-Apr-03 20:18
Stephane Rodriguez.2-Apr-03 20:18 
AnswerPixel access in a Bitmap Pin
R Cotter3-Jul-03 7:30
R Cotter3-Jul-03 7:30 
AnswerOpenGL Vertex Buffers Pin
Sander van Driel3-Sep-04 2:07
Sander van Driel3-Sep-04 2:07 
AnswerRe: The uses of unsafe code? Pin
Le_MuLoT8-Sep-05 3:52
Le_MuLoT8-Sep-05 3:52 
GeneralRe: The uses of unsafe code? Pin
Member 82228581-Sep-15 7:49
Member 82228581-Sep-15 7:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.