Click here to Skip to main content
13,256,123 members (54,432 online)
Click here to Skip to main content
Add your own
alternative version


82 bookmarked
Posted 29 May 2002

Unsafe programming in C#

, 29 May 2002
Rate this:
Please Sign up or sign in to vote.
Discusses the concept of using pointers in C#
<!-- Add the rest of your HTML here -->


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:

public unsafe void Triple(int *pInt)

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:

using System;
class CData
    public int x;

class CProgram
    unsafe static void SetVal(int *pInt)
    public unsafe static void Main()
        CData d = new CData();
        Console.WriteLine("Previous value: {0}", d.x);
        fixed(int *p=&d.x)
        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!


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


About the Author

Kumar Gaurav Khanna
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 (, Developer 2.0 (, and PC Quest (

You may also be interested in...

Comments and Discussions

QuestionImage processing: C# or C++ Pin
VHGN18-Apr-12 0:02
memberVHGN18-Apr-12 0:02 
QuestionHow to implement direct access memory in c# Pin
Milad.D20-May-11 22:49
memberMilad.D20-May-11 22:49 
GeneralTake a look at this pointer tutorial Pin
Theo Bebekis14-Jul-09 3:47
memberTheo Bebekis14-Jul-09 3:47 
GeneralMy vote of 1 Pin
Theo Bebekis14-Jul-09 3:39
memberTheo Bebekis14-Jul-09 3:39 
GeneralMy inspiration is ticked off! Pin
Windmiller28-Aug-07 23:13
memberWindmiller28-Aug-07 23:13 
QuestionHow to get a pointer to an int array [modified] Pin
Georgi Petrov25-Aug-06 3:30
memberGeorgi Petrov25-Aug-06 3:30 
GeneralDisplaying Pointer value Pin
Soulblazer30-Jun-05 22:33
memberSoulblazer30-Jun-05 22:33 
GeneralGetting the pointer value Pin
Anonymous30-Jun-05 22:30
sussAnonymous30-Jun-05 22:30 
GeneralDllImport Or MC++ wrapper Pin
4space14-Nov-02 2:15
member4space14-Nov-02 2:15 
QuestionThe uses of unsafe code? Pin
AK6-Aug-02 7:58
memberAK6-Aug-02 7:58 
AnswerRe: The uses of unsafe code? Pin
Code Boy7-Oct-02 20:22
memberCode Boy7-Oct-02 20:22 
GeneralRe: The uses of unsafe code? Pin
Anonymous9-Oct-02 4:49
sussAnonymous9-Oct-02 4:49 
GeneralRe: The uses of unsafe code? Pin
The Anomaly5-Dec-03 9:04
memberThe Anomaly5-Dec-03 9:04 
GeneralRe: The uses of unsafe code? Pin
The Anomaly5-Dec-03 9:06
memberThe Anomaly5-Dec-03 9:06 
AnswerRe: The uses of unsafe code? Pin
Anonymous20-Dec-02 3:36
sussAnonymous20-Dec-02 3:36 
GeneralRe: The uses of unsafe code? Pin
Anonymous20-Dec-02 3:38
sussAnonymous20-Dec-02 3:38 
GeneralRe: The uses of unsafe code? Pin
.S.Rod.2-Apr-03 21:18
member.S.Rod.2-Apr-03 21:18 
AnswerPixel access in a Bitmap Pin
R Cotter3-Jul-03 8:30
memberR Cotter3-Jul-03 8:30 
AnswerOpenGL Vertex Buffers Pin
Sander van Driel3-Sep-04 3:07
memberSander van Driel3-Sep-04 3:07 
AnswerRe: The uses of unsafe code? Pin
Le_MuLoT8-Sep-05 4:52
memberLe_MuLoT8-Sep-05 4:52 
GeneralRe: The uses of unsafe code? Pin
Member 82228581-Sep-15 8:49
memberMember 82228581-Sep-15 8: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 30 May 2002
Article Copyright 2002 by Kumar Gaurav Khanna
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid