hi,
Sir i want to know what is the unsafe code in .net and how much it different from normal code ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Apr 15, 2012, 6:11 PM
VulpesPosted Apr 15, 2012, 10:12 AM
Whenever you access an array or string element, 'safe' .NET code checks that you're not trying to access an element which is outside the bounds of the array or string. Unsafe code does not do any bounds checking - hence the speed improvement. However, you'll get no warning if you do access 'out of bounds' memory and so this technique needs to be used with great care.
Satyapriya NayakPosted Apr 15, 2012, 6:11 AM
The .NET intermediate language (MSIL) allows expressing both statically verifiable memory and type safe code (typically called managed), as well as unsafe code using direct pointer manipulations. Unsafe code can be expressed in C# by marking regions of code as unsafe. Writing unsafe code can be useful where the rules of managed code are too strict. The obvious drawback of unsafe code is that it opens the door to programming errors typical of C and C++, namely memory access errors such as buffer overruns. Worse, a single piece of unsafe code may corrupt memory and destabilize the entire runtime or allow attackers to compromise the security of the platform.
We present a new static analysis based on abstract interpretation to check memory safety for unsafe code in the .NET framework. The core of the analysis is a new numerical abstract domain, Strp, which is used to efficiently compute memory invariants. Strp is combined with lightweight abstract domains to raise the precision, yet achieving scalability.
We implemented this analysis in Clousot, a generic static analyzer for .NET. In combination with contracts expressed in FoxTrot, an MSIL based annotation language for .NET, our analysis provides static safety guarantees on memory accesses in unsafe code. We tested it on all the assemblies of the .NET framework. We compare our results with those obtained using existing domains, showing how they are either too imprecise (e.g., Intervals or Octagons) or too expensive (Polyhedra) to be used in practice.
unsafe code or in other words unmanaged code it is possible to declare and use pointers. But the question is why do we write unmanaged code? If we want to write code that interfaces with the operating system, or want to access memory mapped device or want to implement a time critical algorithm then the use of pointer can give lots of advantages.
But there are some disadvantages of using pointers too. If pointers were chosen to be 32 bit quantities at compile time, the code would be restricted to 4gig of address space, even if it were run on a 64 bit machine. If pointers were chosen at compile time to be 64 bits, the code could not be run on a 32 bit machine.
In C# we can write unsafe code with the modifier unsafe. All the unsafe code must be clearly marked with the unsafe modifier. Writing unsafe code is much like writing C code in a C# program.
using System;
class MyClass {
public static void Main() {
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " + (int)pData );
}
}
Here I use a pointer in this program. Now compile this program. The compiler gives the error
Microsoft (R) Visual C# Compiler Version 7.00.9030 [CLR version 1.00.2204.21]
Copyright (C) Microsoft Corp 2000. All rights reserved.
um1.cs(6,8): error CS0214: Pointers may only be used in an unsafe context
um1.cs(8,27): error CS0214: Pointers may only be used in an unsafe context
Please refer the below links
http://msdn.microsoft.com/en-us/library/aa288474%28v=vs.71%29.aspx
http://www.c-sharpcorner.com/UploadFile/051e29/unsafe-code-in-C-Sharp/
http://www.codeproject.com/Articles/1099/Writing-Unsafe-code-using-C
Thanks