0% found this document useful (0 votes)
20 views13 pages

Pointer (Unsafe Code)

Pointer(Unsafe Code) Pointer variables in C# store memory addresses of other variables and data types. Code using pointers must run in an "unsafe" context to access memory without garbage collection. Pointers can reference values types, arrays, and structures containing value types. Pointer arithmetic allows accessing array elements or structure fields using pointer notation. Unsafe code must be enabled in projects using pointers.

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

Pointer (Unsafe Code)

Pointer(Unsafe Code) Pointer variables in C# store memory addresses of other variables and data types. Code using pointers must run in an "unsafe" context to access memory without garbage collection. Pointers can reference values types, arrays, and structures containing value types. Pointer arithmetic allows accessing array elements or structure fields using pointer notation. Unsafe code must be enabled in projects using pointers.

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Pointer(Unsafe Code)

What is Pointer ?
• C# supports pointers in a limited extent.

• A C# pointer is nothing but a variable that holds the


memory address of another type.

• A pointer is a variable whose value is the address of


another variable

• But in C# pointer can only be declared to hold the


memory address of value types and arrays.
• The general form of a pointer declaration is −
– type *var-name;

• Following are valid pointer declarations −


– int *ip; /* pointer to an integer */
– double *dp; /* pointer to a double */
– float *fp; /* pointer to a float */
– char *ch /* pointer to a character */
Using Unsafe Code in C#
• 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 outside the control of Garbage Collector.

• Remember that in C# any code involving pointers


requires an unsafe context.

• Syntax:
– unsafe Context_declaration
Example1
using System;
class Program
{
static void Main()
{
unsafe
{
int x = 10;
int* ptr;
ptr = &x;
Console.WriteLine("Inside the unsafe code block");
Console.WriteLine("The value of x is " + *ptr);
}

}
}
Example2

using System;
class Program
{
static unsafe void Main()
{
int x = 10;
int* ptr;
ptr = &x;
Console.WriteLine("Inside the unsafe code block");
Console.WriteLine("The value of x is " + *ptr);
}
}
Example of method
using System; class demo
class Program {
{ static void Main(string[] args)
public void show() {
{ Program p1=new Program();
unsafe p1.show();
{ }
int x = 10; }
int* ptr;
ptr = &x;
Console.WriteLine("Inside the unsafe code block");
Console.WriteLine("The value of x is " + *ptr);
}
}
}
Example of swap two value
using System;
class TestPointer
{
public unsafe void swap(int* p, int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
public unsafe static void Main()
{
TestPointer p = new TestPointer();
int var1 = 10;
int var2 = 20;
int* x = &var1;
int* y = &var2;

Console.WriteLine("Before Swap: var1:{0}, var2: {1}", var1, var2);


p.swap(x, y);
Console.WriteLine("After Swap: var1:{0}, var2: {1}", var1, var2);
}
}
Pointer to Array
• In C# array elements can be accessed by using
pointer notations.

• Therefore, if you need to access an array data


using a pointer variable, you need to fix the
pointer using the fixed keyword.
Example
using System;
class TestPointer
{
public unsafe static void Main()
{
int[] list = {10, 100, 200};
fixed(int *ptr = list)

for ( int i = 0; i < 3; i++)


{
Console.WriteLine("Address of list[{0}]={1}",i,(int)(ptr+i));
Console.WriteLine("Value of list[{0}]={1}", i, *(ptr+i));
}
}
}
Pointer to structure
• The structures in C# are value types.

• The pointers can be used with structures if it


contains only value types as its members.
Example

using System; class MyClient


struct MyStruct
{
{
public int x; public unsafe static void
public int y; Main()
public void SetXY(int i, int j)
{
{
x = i; MyStruct ms = new
y = j; MyStruct();
}
MyStruct* ms1 = &ms;
public void ShowXY()
{ ms1->SetXY(10, 20);
Console.WriteLine(x); ms1->ShowXY();
Console.WriteLine(y);
}
}
} }
• Compiling Unsafe Code-CMD
• For compiling unsafe code, you have to specify the /unsafe
command-line switch with command-line compiler.
• For example, to compile a program named prog1.cs containing
unsafe code, from command line, give the command −
• csc /unsafe prog1.cs

• If you are using Visual Studio IDE then you need to enable use of
unsafe code in the project properties.
• To do this −
• Open project properties by double clicking the properties node in
the Solution Explorer.
• Click on the Build tab.
• Select the option "Allow unsafe code".

You might also like