Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
World Class ASP.NET Hosting – Click Here for 3 Months Free/NO Setup Fee!
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » C# Language » Pointers in C#

Pointers in C#

C# also supports pointers in a limited extent. A pointer is nothing but a variable that holds the memory address of another type. But in C# pointer can only be declared to hold the memory address of value types and arrays.

Author Rank:
Total page views :  87148
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

C# also supports pointers in a limited extent. A pointer is nothing but a variable that holds the memory address of another type. But in C# pointer can only be declared to hold the memory address of value types and arrays. Unlike reference types, pointer types are not tracked by the default garbage collection mechanism. For the same reason pointers are not allowed to point to a reference type or even to a structure type which contains a reference type. We can say that pointers can point to only unmanaged types which includes all basic data types, enum types, other pointer types and structs which contain only unmanaged types. 

Declaring a Pointer type 

The general form of declaring a pointer type is as shown below        

type *variable_name; 

Where * is known as the de-reference operator. For example the following statement

int *x ; 

Declares a pointer variable x, which can hold the address of an int type. The reference operator (&) can be used to get the memory address of a variable. 

int x = 100;

The &x gives the memory address of the variable x, which we can assign to a pointer variable 

int *ptr = & x;.
Console.WriteLine((
int)ptr) // Displays the memory address
Console.WriteLine(*ptr) // Displays the value at the memory address.

Unsafe Codes 

The C# statements can be executed either as in a safe or in an unsafe context. The statements marked as unsafe by using the keyword unsafe runs out side the control of Garbage Collector. Remember that in C# any code involving pointers requires an unsafe context. 

We can use the unsafe keyword in two different ways. It can be used as a modifier to a method, property, and constructor etc. For example         

// Author: rajeshvs@msn.com
using System;
class MyClass
{
public unsafe void Method()
{
int x = 10;
int y = 20;
int *ptr1 = &x;
int *ptr2 = &y;
Console.WriteLine((
int)ptr1);
Console.WriteLine((
int)ptr2);
Console.WriteLine(*ptr1);
Console.WriteLine(*ptr2);
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.Method();
}

 
The keyword unsafe can also be used to mark a group of statements as unsafe as shown below.         

// Author: rajeshvs@msn.com
//unsafe blocks
using System;
class MyClass
{
public void Method()
{
unsafe
{
int x = 10;
int y = 20;
int *ptr1 = &x;
int *ptr2 = &y;
Console.WriteLine((
int)ptr1);
Console.WriteLine((
int)ptr2);
Console.WriteLine(*ptr1);
Console.WriteLine(*ptr2);
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.Method();
}
}

Pinning an Object

The C# garbage collector can move the objects in memory as it wishes during the garbage collection process. The C# provides a special keyword fixed to tell Garbage Collector not to move an object. That means this fixes in memory the location of the value types pointed to. This is what is known as pinning in C#. 

The fixed statement is typically implemented by generating tables that describe to the Garbage Collector, which objects are to remain fixed in which regions of executable code. Thus as long as a Garbage Collector process doesn't actually occur during execution of fixed statements, there is very little cost associated with this. However when a Garbage Collector process does occur, fixed objects may cause fragmentation of the heap. Hence objects should be fixed only when absolutely necessary and only for the shortest amount of time.

Pointers & Methods 

The points can be passed as argument to a method as showing below. The methods can also return a pointer.         

// Author: rajeshvs@msn.com
using System;
class MyClass
{
public unsafe void Method()
{
int x = 10;
int y = 20;
int *sum = swap(&x,&y);
Console.WriteLine(*sum);
}
public unsafe int* swap(int *x, int *y)
{
int sum;
sum = *x + *y;
return ∑
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.Method();
}
}

Pointers & Conversions

In C# pointer types do not inherit from object and no conversion exists between pointer types and objects. That means boxing and un-boxing are not supported by pointers. But C# supports conversions between the different pointer types and pointer types and integral types. 

C# supports both implicit and explicit pointer conversions within un-safe context. The implicit conversions are

  1. From any type pointer type to void * type.
  2. From null type to any pointer type.

The cast operator (()) is necessary for any explicit type conversions. The explicit type conversions are

  1. From any pointer type to any other pointer type.
  2. From sbyte, byte, short, ushort, int, uint, long, ulong to any pointer type.
  3. From any pointer type to sbyte, byte, short, ushort, int, uint, long, ulong types.

For example

char c = 'R';
char *pc = &c;
void *pv = pc; // Implicit conversion
int *pi = (int *) pv; // Explicit conversion using casting operator

Pointer Arithmetic

In an un-safe context, the ++ and - operators can be applied to pointer variable of all types except void * type. Thus for every pointer type T* the following operators are implicitly overloaded.

T* operator ++ (T *x);
T*
operator -- (T *x);

The ++ operator adds sizeof(T) to the address contained in the variable and - operator subtracts sizeof(--) from the address contained in the variable for a pointer variable of type T*.

In an un-safe context a constant can be added or subtracted from a pointer variable. Similarly a pointer variable can be subtracted from another pointer variable. But it is not possible to add two pointer variables in C#. 

In un-safe context = =, ! =, <, >, < =, > =  operators can be applied to value types of all pointer types. The multiplication and division of a pointer variable with a constant or with another pointer variable is not possible in C#. 

Stack Allocation 

In an unsafe context, a local declaration may include a stack allocation initialiser, which allocates memory from the call stack.

The stackalloc T[E] requires T to be an unmanaged type and E to be an expression of type int. The above construct allocates E * sizeof(T) bytes from the call stack and produces a pointer of the type T* to the newly allocated block. The E is negative, a System.OverFlowException is thrown. If there is not enough memory to allocate, a System.StackOverflowException is thrown. 

The content of newly allocated memory is undefined. There is no way to implicitly free memory allocated using stackalloc. Instead all stack allocated memory block are automatically discarded once the function returns. 

class Test
{
char *buffer =
}

Pointers & Arrays 

In C# array elements can be accessed by using pointer notations.

// Author: rajeshvs@msn.com
using System;
class MyClass
{
public unsafe void Method()
{
int []iArray = new int[10];
for(int count=0; count < 10; count++)
{
iArray[count] = count*count;
}
fixed(int *ptr = iArray)
Display(ptr);
//Console.WriteLine(*(ptr+2));
//Console.WriteLine((int)ptr);
}
public unsafe void Display(int *pt)
{
for(int i=0; i < 14;i++)
{
Console.WriteLine(*(pt+i));
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.Method();
}
}

Pointers & Structures 

The structures in C# are value types. The pointers can be used with structures if it contains only value types as its members. For example 

// Author: rajeshvs@msn.com
using System;
struct MyStruct
{
public int x;
public int y;
public void SetXY(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine(x);
Console.WriteLine(y);
}
}
class MyClient
{
public unsafe static void Main()
{
MyStruct ms =
new MyStruct();
MyStruct *ms1 = &ms;
ms1->SetXY(10,20);
ms1->ShowXY();
}
}


Login to add your contents and source code to this article
 About the author
 
Rajesh VS
Rajesh V.S is a software engineer in the area of C/C++/JAVA for the last 5 years. Currently he is interested in core C# language. He is Sun Certified Java Programmer. His other area of interest includes Design Patterns and CORBA. He is also a writer of numerous articles for many technical Web sites.
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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
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.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Posting images by kalyan On May 29, 2009

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.