Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » C# Language » Writing unsafe code - pointers in C#

Writing unsafe code - pointers in C#

In this article I will give a short description of one of the feature of C# which are pointers and so-called unsafe code. This subject is particularly close for C++ programmers. Moreover, it is a feature that we do not find in Java.

Page Views : 73690
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
Mindcracker MVP Summit 2012
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Summary

In this article I will give a short description of one of the feature of C# which are pointers and so-called unsafe code. This subject is particularly close for C++ programmers. Moreover, it is a feature that we do not find in Java.

Managed code

Generally speaking when you write any program in C# you create managed code. Managed code is executed under the control of Common Language Runtime (CRL). CRL causes that programmer do not need to manage memory and take care about memory's allocation and deallocation. CLR also allows you to write what is called 'unsafe code'.

Unsafe code

Unsafe code is code which does not executed under full control of CLR. It can cause some problems therefore each use must be marked as "unsafe".

...

unsafe
{
...
// unsafe context: can use pointers here
...
}
...
 
Keyword 'unsafe' might be also used in other cases for example we can mark class or method as unsafe: 

unsafe class Class1 {}
static unsafe void FastMove ( int* pi, int* pdi, int length) {...}
 

Necessity of using the 'unsafe' keyword protects programmers before its accidental usage. If code is unsafe you might ask yourself why somebody want to use it. The answer is that from time to time, in some cases, it is necessary to use pointers.

Pointers

Pointers are special kind of variables which hold addresses other variables. If you assign address first variable to second variable you can say that first variable 'point at' the second one. There are three kinds of pointers supported by the CRL: managed pointers, unmanaged pointers and unmanaged function pointers. Managed pointers are references to managed blocks of memory which are stored on the heap. An unmanaged pointers are traditional C++ pointers and each use must be placed in unsafe block of code. An unmanaged function pointers are also the traditional C++ pointers that refer to the addresses of the function (delegates may be treat as unmanaged function pointers).

To create a pointer you can use the following declaration:

type* variable_name; 

As a type may be used each type that is not a reference-type and does not contain reference-type field. It can be only: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool any enum-type and other pointer-type. Any user-defined struct-type that contains fields of unmanaged-types can be also used.

The following examples show different pointers declarations:

int* pi //declaration a pointer to integer variable
float* pf, pq // two pointers to float variables. Not *pf, *pq
char* pz // pointer to char

As mentioned earlier the unnamed code is not verifiable by CRL and to compile it you must specify /unsafe compiler option. If you use Microsoft Visual Studio you should set to true 'Allow unsafe code block' option in project property window.

Project property window.

Basic using of pointers

There is also other operator which is inseparably linked with pointers. It is & operator. The & operator returns the memory address of its operand.

For example:
unsafe
{
int* pi;
int x = 1;
pi = &x;
System.Console.WriteLine("Value of x
is: " + *pi);
}
 
In this example we created two variables, 'pi' as pointer to int and 'x' as int. Then we put into 'pi' the memory address of the variable 'x'. It is very important to understand that in 'pi' variable we put address to 'x', not 'x' value (using: pi = x returns error "Cannot implicitly convert type 'int' to 'int*'")

After compilation and running the output will be:

Value of x is: 1 

Pointers accept null values, there is also possible to use pointers which type is void.
The following code will be compile correctly.

unsafe
{
nt x = 10;
void* px = &x;
double *pd = (double*)px;
}
 
Keyword fixed and garbage collector

Using pointers in C# require much more attention then in C++. That is because of garbage collector (g.c.) which can run memory cleaning. During cleaning, g.c. can change physical position of the objects. If g.c. changes position of an object the pointer will point at wrong place in memory. To avoid such problems (connected with garbage collector) C# contains 'fixed' keyword. It informs system not to relocate an object by the garbage collector.

Examples of 'fixed':

// pt is a managed variable, subject to g.c.
Colour cl = new Colour();
// must use fixed to get address of cl.R
fixed ( int* pi = &cl.R)
{
*pi = 1;
}

To initialize multiple pointers all of the same type:

fixed (byte* pb = sarr, pd = darr) {...}

To initialize pointers of different type:

fixed (int* pi = &cl.G)
fixed (double* pd = &array[10])

If we forget 'fixed' keyword the compiler will warn us of our mistake but it is not so intelligent to warn us in the following situation. There is a serious bug in the following code in spite of the fact the compilation ends successful.

class Test
{
public int x;
}
unsafe class SimpleTest
{
[STAThread]
static void Main(string[] args)
{
Test test =
new Test();
int* pi;
fixed (int* px = &test.x)
{
*px = 100;
pi = px;
}
Console.WriteLine("before g.c.: " + *pi);
System.GC.Collect(2);
Console.WriteLine("after g.c.: " + *pi);
}
}

On my computer results were:

before g.c.: 100
after g.c.: 132 

As we have seen there were two different values for the same pointer. In reality there is very little probability of getting difference between 'before g.c.' and 'after g.c.' because probability of starting garbage collector is very little. But as a rule we should avoid using pointers outside fixed block. In our case each use of pointer 'pi' outside fixed block might provide errors very difficult to diagnose.

C# pointers and WinApi

The most important advantage of using pointers is cooperation with binary code.
Many WinApi functions use pointers. For example GetComputerName (Kernel32.lib.) provides our computer name.

BOOL GetComputerName(LPTSTR lpBuffer, // computer name
LPDWORD lpnSize // size of name buffer);

Using of the GetComputerName demonstrate the following program
[System.Runtime.InteropServices.DllImport("Kernel32")]
static extern unsafe bool GetComputerName(byte* lpBuffer,long* nSize);
static void Main()
{
byte[] buffor = new byte[512];
long size = buffor.Length;
unsafe
{
long* pSize = &size;
fixed (byte* pBuffor = buffor)
{
GetComputerName(pBuffor,pSize);
}
}
System.Text.Encoding textEnc =
new System.Text.ASCIIEncoding();
System.Console.WriteLine("Computer name: {0}",textEnc.GetString(buffor));
}

Conclusion

As we have seen pointers are very useful part of C# language. Using pointers are not difficult but should be used carefully because it might be cause of errors difficult to diagnose. Using pointers disturb garbage collector functioning especially when we use many pointers in our program. Therefore before using pointers we should consider it and maybe try to find some other ways.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
gregory_popek
I work as software designer in the East-Europe. I graduated physics with computer science and also computer engineering. By the last three years I had been working in .Net team as software developer.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.