Pointers and Unsafe Coding in C#
C# is a language that integrates useful concepts from several programming languages. C# contains advanced features of Java and provided C++s robustness. The concept of pointer is integral in C and other low level languages. However, C# also allows you to utilize the power of pointers in your code via unsafe code and pointer options.
When you write the following line of code
Pointer is a different concept and is often times considered complicated. However, with careful attention to this article and with little bit of practice you can easily grasp the concept and implement it in your code.
In order to use pointers in your C# code, you need remember the following three operators. They are vital to using pointers in C#.
This is denoted by ampersand &. This operator returns the pointer to the memory address of the variable.
This is denoted by a steric *. This operator has the functionality opposite to that of address-of operator. It returns the variable located at the address of the pointer.
This denoted by ->. Actually pointer-to-member function is a shorthand notation in which x->y is actually equal to (*x).y, which means that return the variable located at pointer x and multiply it with y.
Now you would have understood the basics of pointer. Another very important point to be noted here is that whenever you use pointers in your code you will have to enclose them in unsafe block and you will have to compile that code with unsafe compiler option. When you mark a block of code unsafe, you are actually telling the compiler that allow the pointer variables to manipulate the memory address of the program, which are within the unsafe code block. Since you are directly manipulating the memory addresses when you use pointers, you will have to declare that block of code unsafe. However, an unsafe code is faster in terms of performance as compared to ordinary safe code. Now we will move towards our first example which elaborates the use of pointers in C#. Have a look at the first Example.
This means that a can hold the memory address of any variable or literal of type integer. In the next line we assigned the memory address of integer b to pointer a using the address-of operator &. Now the pointer a contains the memory address of variable b which means that basically memory address of variable b can now be accessed via b as well as the pointer a.
Now, in order to access the memory address stored in the pointer a you will simply have to access pointer a. However, if you want to get the value that is stored in the memory address to which our pointer a is referring, you will have to use dereference operator steric * which will fetch the value that is stored in memory address.
Again coming back to our code in Example1, we have initialized the integer type variable b with value 20. As pointer a points to the memory address of b therefore the value stored in the memory address of pointer a should also be 20. In the next lines we have displayed the value of b, the value stored in the memory address of pointer a and the memory address of pointer a which will be any number. The output shows that both the value of b and pointer a would be the same.
In the next line we have changes the value stored in the memory address contained by pointer a. We have changed it to 10. Since the memory address stored in pointer is the memory address of the integer variable b, now the value of b will also be 10. We have again printed the value of b, the value stored in the memory address contained by pointer a and the memory address of a. You will see that new values of b and a would be 10. That shows that changing the value of a also changes the value of b.
Next we have proved that changing the value of b would change the value of a. We have now assigned b the value 25. And now if we print the value of b and the value stored in the memory address contained by pointer it would be same i.e 25.
We mentioned earlier in the article that in order to use pointers in the code, we will have to allow the compiler to compile the unsafe code. We wrote our code in VS 2010. In order to compile unsafe code in visual studio, do the following steps:
Pointers are also used to pass parameters by reference to the methods. Before explaining how pointers do this, let us first discuss difference between passing parameters by value and passing by reference.
When you pass the argument to a normal method, a copy of the arguments is locally generated whose scope is within the method to which variables are being passed. If method modifies these copies of the arguments, the actual arguments dont get modified. This is called passing by reference.
Passing by reference is a different in a sense that instead of passing the values of the parameters, the reference to the parameters is passed to the method. The method actually operates on the reference of the parameters being passed. Therefore, if the function modifies these values, they are also modified in the calling function.
We will take help of an example to further elaborate this concept that how pointers are used in passing parameters by reference to a function. Have a look at Example2.
But how values have been swapped? We havent returned anything. This is the beauty of using pointers, you didnt returned anything yet you are able to manipulate the variables in the calling function because you do not create a copy of the passed parameters, rather you use a reference of those variable and hence the original values of those variables is also modified.
Another huge advantage of using pointers is that it allows you to have a workaround for returning multiple variables from a function. Normally, a function can only return single value when you pass parameters by value. However, when you pass parameters by reference you can manipulate multiple numbers of variables in the calling function or in other words, you are returning multiple values from a function.
Inside the main method we have declared two variables, namely num1 and num2. We have stored 25 and 50 in both of these integer type variables respectively. We then declared two pointers a and b and stored the memory address of integer variables num1 and num2 in these pointers, respectively. We then displayed the values of both num1 and num2. At that time num1 would contain 25 and num2 would contain 50. We then called the swapvalues method and passed the pointers a and b to the method as reference parameters. The swapvalues method would swap the values as discussed earlier. Then we have again displayed the values of integer variables num1 and num2 which would now contain swapped values 50 and 25. The output of Example2 is as follows.
We have explained all the basic pointer concepts except the usage of pointer to member operator ->. This is mostly used with structs and classes. We will explain this with the help of an example, how this can be used in your code.
|All times are GMT +5.5. The time now is 23:57.|