Unsafe code and the use of pointer types

In languages such as C or C++, developers have the features to create pointers or *, which is an object that stores the memory address of another variable. This object allowed very low-level access of the memory to the application. However, due to the possibility of dangling pointers, the performance of the application suffers greatly. A dangling pointer is a potential situation that could exist in C when a pointer object is still pointing to a memory location that is no longer allocated in the application. Please refer to the following diagram:

In the diagram, we have an application running in C or C++ that declared Pointer B and pointed it to the memory address of variable A. The pointer saves the memory address of the variable. Hence, in other words, Pointer B will not contain the memory address of variable A. Now, at some point during the program run, memory location A was released by the application. Even though the memory was released, there could be circumstances when we do not explicitly clear out the contents of the pointers containing the respective memory address. Due to this mistake or oversight, Pointer B was not updated to point to a new block of memory or pointing it to null. As a result, the pointer is still referring to a memory location that no longer exists in the application. This situation is called dangling pointers.

C# removes the possibility of dangling pointers because, explicitly, it does not allow the use of pointers. Instead, it encourages people to use reference types. The memory management of reference types is managed by a garbage collector.

In Chapter 9, Managing the Object Life Cycle, we will look further at how the garbage collector works in .NET. 

However, there are still some circumstances when developers feel the need to use pointers in their C# application. This is useful in scenarios where we need to do some operations with the underlying operating system, such as Windows or Linux, in which the application is running. In such circumstances, we will need pointers. To cater to such scenarios, C# has the concept of unsafe code in which it allows developers to use pointers in their code. The code that uses pointers must be classified clearly with an identifier of unsafe. This keyword conveys the message to Common Language Runtime (CLR) that the code block is unmanaged or unsafeā€”or, in other words, has used pointers. Let's go through a code example to see how we use pointer types in C#.

In the code example, we are creating a function block in which we are using a pointer variable. We will be saving the address of an int type in an int pointer type variable. Please refer to the following screenshot. Note that the user gets an error when they try to compile the program:

The reason is that, by default, the C# compiler will not allow any code containing a pointer or unsafe code block to be executed. We can override this behavior of C# by using the unsafe keyword in the function block:

class Program
{
static void Main(string[] args)
{
UnSafeExample();
}
unsafe static private void UnSafeExample()
{
int i = 23;
int* pi = &i;
Console.WriteLine(i);
Console.WriteLine(*pi);
Console.ReadLine();
}
}

To allow the compilation of unsafe code, we will need to change the build setting in Visual Studio. To update the settings, we need to right-click on the Project and click on Properties. Now, navigate to the Build section. Please refer to the following screenshot, which highlights the Visual Studio setting that we need to specify to allow compilation of unsafe code:

Now we have revisited the different types possible in C#. The next section explains the guiding principles that help us select a particular variable type over another.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.82.21