Using Unsafe Code in C#
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   Unsafe Code
Current Lesson:
Using Unsafe Code in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  ListBox Control Overview [next Lesson]  Selecting a Random Row
Using Unsafe Code in C#
  by: Mardawi

Using Unsafe Code in C#

Using Unsafe Code – Part 1

C# .NET hides most of memory management, which makes it much easier for the developer. Thanks for the Garbage Collector and the use of references. But to make the language powerful enough in some cases in which we need direct access to the memory, unsafe code was added.

In the first part of this article I will go throw why (also not) and how to use unsafe code which is mostly about pointers. The second part I will go more deep in pointers, and I will provide examples about where to use them.

Commonly while programming in the .net framework we don’t need to use unsafe code, but in some cases there is no way not to, such as the following:

  • Real-time applications, we might need to use pointers to enhance performance in such applications.
  • External functions, in non-.net DLLs some functions requires a pointer as a parameter, such as Windows APIs that were written in C.
  • Debugging, sometimes we need to inspect the memory contents for debugging purposes, or you might need to write an application that analyzes another application process and memory.

Unsafe code as I said before is mostly about pointers which have the following advantages and disadvantages.

Advantages:

  • Performance and flexibility, by using pointer you can access data and manipulate it in the most efficient way possible.
  • Compatibility, in most cases we still need to use old windows APIs, which use pointers extensively. Or third parties may supply DLLs that some of it’s functions need pointer parameters. Although this can be done by using DLLImport and System.IntPtr class, but in some cases it’s just much simpler to use pointer.
  • Memory Addresses, there is no way to know the memory address of some data without using pointers.

Disadvantages:

  • Complex syntax, to use pointers you need to go throw more complex syntax than we used to experience in C#.
  • Harder to use, you need be more careful and logical while using pointers, miss using pointer might lead to the following:
    • Overwrite other variables.
    • Stack overflow.
    • Access areas of memory that doesn’t contain any data as they do.
    • Overwrite some information of the code for the .net runtime, which will surely lead you application to crash.
  • Type-safety, using pointers will cause the code to fail in the .net type-safety checks, and of course if your security police don’t allow non type-safety code, then the .net framework will refuse to execute it.

After we knew all the risks that might face us while using pointer and all the advantages those pointers introduces us of performance and flexibility let find how to use them.

The keyword unsafe is used while dealing with pointer, the name reflects the risks that you might face while using them. Let’s see where to place it.

We can declare a whole class as unsafe:

unsafe class Class1

{

//you can use pointers here!

}

 

Or only some class members can be declared as unsafe:

 

class Class1

{

//pointer

unsafe int * ptr;

 

unsafe void MyMethod()

{

//you can use pointers here

}

}

The same applies to other members such as the constructor and the properties.

To declare unsafe local variables in a method, you have to put them in unsafe blocks as the following:

static void Main()

{

//can't use pointers here

unsafe

{

//here you can declare and use pointer

}

//can't use pointers here

}

 

You can’t declare local variables the same way we used in declaring global variables, to use pointers, and next we will use the operator sizeof, in a “safe” method. We have to put them in an unsafe block.

 

static void Main()

{

unsafe int * ptri; //Wrong

}

If you got too excited and tried to use unsafe then when you compile the code just by using

csc test.cs

You will experience the following error:

 

error CS0227: Unsafe code may only appear if compiling with /unsafe

 

For compiling unsafe code use the /unsafe

 

csc test.cs /unsafe

 

 

In VS.NET go to the project property page and in “configuration properties>build” set Allow Unsafe Code Blocks to True.

 

After we knew how to declare a block as unsafe we should now learn how to declare and use pointers in it.

 

Declaring pointers

To declare a pointer of any type all what you have to do is to put ‘*’ after the type name such as

 

int * ptri;

double * ptrd;

 

NOTE: If you used to use pointer in C or C++ then be careful that in C# int * ptri, i; ‘*’ applies to the type itself not the variable so ‘i’ is a pointer here, same as arrays.

 

Using pointers

 

Using pointers can be demonstrated in the following example:

 

static void Main()

{

int var1 = 5;

unsafe

{

int * ptr1, ptr2;

ptr1 = &var1;

ptr2 = ptr1;

*ptr2 = 20;

}

Console.WriteLine(var1);

}

 

The operator ‘&’ means “address of”, so ptr1 will hold the address of var1, ptr2 = ptr1 will assign the address of var1, which ptr1 was holding, to ptr2. Using ‘*’ before the pointer name means “the contain of the address”, so 20 will be written where ptr2 points.

Now var1 value is 20.

 

sizeof operator

 

As the name says, sizeof operator will return the number of bytes occupied of the given data type (could also be used for classes and structs, we will see that in part 2)

i.e.

 

unsafe

{

Console.WriteLine("sbyte: {0}", sizeof(sbyte));

Console.WriteLine("byte: {0}", sizeof(byte));

Console.WriteLine("short: {0}", sizeof(short));

Console.WriteLine("ushort: {0}", sizeof(ushort));

Console.WriteLine("int: {0}", sizeof(int));

Console.WriteLine("uint: {0}", sizeof(uint));

Console.WriteLine("long: {0}", sizeof(long));

Console.WriteLine("ulong: {0}", sizeof(ulong));

Console.WriteLine("char: {0}", sizeof(char));

Console.WriteLine("float: {0}", sizeof(float));

Console.WriteLine("double: {0}", sizeof(double));

Console.WriteLine("decimal: {0}", sizeof(decimal));

Console.WriteLine("bool: {0}", sizeof(bool));

//did i miss something?!

}

 

the output will be:

sbyte: 1

byte: 1

short: 2

ushort: 2

int: 4

uint: 4

long: 8

ulong: 8

char: 2

float: 4

double: 8

decimal: 16

bool: 1

 

 

Great, we don’t have to remember the size of every data type anymore!

That’s it for part one, in the second part I will cover the following subjects:

  • Casting pointers
  • Pointer arithmetic
  • Pointers to classes and structs
  • Optimizing performance by using pointers

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  Unsafe Code
Current Lesson:
Using Unsafe Code in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  ListBox Control Overview [next Lesson]  Selecting a Random Row


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
carlos_roc.. 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center