Unmanaged code is the code that is not compiled to operate under CLR.In simple terms these are the block of code that is not managed by the CLR. It can be the native C++ or C with pointers, VB, Delphi etc.. All the .net framework languages are compiled to the IL + metadata + some header info for the CLR = PE. Then this PE is handled by the CLR , which finally generates the code to the assembly language instructions.
The code which follows this process or that is handled by the CLR , only those are called as the managed code, while rest are the unmanaged ones.
So the languages that are compiled by the .net compilers and are handled by the CLR those are the managed ones. The metadata and some header info that got attached to the compiled code are responsible for keeping the managed code managed, managed by the CLR. The CLR controls the definitions and boundaries (by means of CTS and CLS) , controls memory through automatic memory management of data via garbage collector and provide CAS security among other benefits. The unmanaged code uses the unmanaged heap for memory and knows nothing about the CLR. Essentially in both the cases a PE(Portable Executable) format file is generated on compilation, difference lies in the fact that , in .Net, this PE file is called as an assembly and has IL + metadata + some header info along with the normal information. And due to these metadata and info, it is able to be JITed by the CLR to generate the native assembly language.
Also if unmanaged resource is referenced in the code, then it is the responsibility of the programmer to release that resource voluntarily when it is not in further use.
UNSAFE CODE :
In .net we have another term called “unsafe code”. In .net most of the memory management is hidden. But sometimes its essential to refer to the memory directly by means of pointers. So to make the language powerful , unsafe code was added. It basically involves the usage of pointers to refer to the memory directly.
We may need pointers to enhance performance in following applications , to name a few. But in general we don’t need to use unsafe codes in .net.
• 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.
Pointers have their own set of advantages and disadvantages, we won’t be discussing that over here.
The keyword unsafe is used where ever we want to use pointers. lets see how we can use:
Whole class can be declared as unsafe
unsafe class classA
{
//can use pointers here.
}
some class members can also be declared as unsafe instead of the whole class.
Class classA
{
//pointer
Unsafe int *ptr;
Unsafe void myMethod()
{
//you can use pointers here
}}
Same applies to other members constructors and properties also.
To declare local variables as unsafe in a method, u need to put it in an unsafe block.
static void Main()
{
//can't use pointers here
unsafe
{//here you can declare and use pointer
}
//can't use pointers here
}
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,i;
double * ptrd;
NOTE: In C# int * ptri, i; ‘*’ applies to the type itself not the variable so ‘i’ is a pointer here, same as arrays.
Using pointers
static void Main()
{
int var1 = 5;
unsafe
{
int * ptr1, ptr2;
ptr1 = &var1;
ptr2 = ptr1;
*ptr2 = 50;
}
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 50 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.
unsafe
{
Console.WriteLine("sbyte: {0}", sizeof(sbyte));
Console.WriteLine("byte: {0}", sizeof(byte));
}
Output:
Sbyte:1;byte:1
July 8, 2009
Unmanaged Code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment