Tags

, ,

Hi there, most of the programmers at-least heard about a concept called ‘Pointers’ for which most of us think its belong to C and C++ but 90+% of the programming languages use pointers to address the memory.

The documentation of C++ defines the Pointers as

We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory, we simply used its identifier whenever we wanted to refer to our variable.

The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte). These single-byte memory cells are numbered in a consecutive way, so as, within any block of memory, every cell has the same number as the previous one plus one.

This way, each cell can be easily located in the memory because it has a unique address and all the memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it is going to be right between cells 1775 and 1777, exactly one thousand cells after 776 and exactly one thousand cells before cell 2776.

 

What is reference operator [ & ]

The address that locates a variable within memory is what we call a reference to that variable. This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as “address of”. For example: 

a=&x;

since C# never cares about whitespace

a = & x;

also a valid code.

 

What is de-reference operator [ * ]


Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer’s identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to “value pointed by”.

int* ptr = &x;

and 

int * ptr = & x;

also a valid code in C#.

We can also define like the below

int *p1;
 
*p1 = x;

and these are the basics of programming languages.

Small code using pointers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace PointerBasic
{
    class MyClass
    {
        public void Method()
        {
            unsafe
            {
                int x = 10;
                int * ptr = & x;
                Console.WriteLine((int)ptr);
                *ptr = 2;
                Console.WriteLine(*ptr);
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            MyClass m = new MyClass();
            m.Method();
            Console.ReadLine();
        }
    }
}

In C# the method of the pointer opreation should be unsafe and we define that in two methods

One is 

public unsafe void Method()
{
    //unsafe pointer code here
}

and the next is like our full example

public void Method()
{
    unsafe
    {
        //unsafe pointer code here
    }
    //other safe codes here
}

To run this kind of unsafe codes we need to allow our project to run unsafe codes. This can be done from the properties of the project

  1. Open project properties
  2. Open the “Build” tab
  3. Tick on “Allow unsafe code”

image

Then only our program will run, otherwise it will show error.

Advertisements