Playaround with an address of object reference variable


Well, rarely you will have to find an address of a variable in C#.NET. Though C#.NET allows use of pointers but anyone hardly use them. .NET base library and Compiler have done a beautiful job, abstracting the complex use of pointers, exposing ref, out keywords.

But what if you still want to playaround?
[I must warn you about this. Don’t dare to play with Pointers 🙂 Improper handling of pointers can even bring your application down]

Thankfully, there are constructs which do help in achieving what we want.

The above link is just to get a concept of Stack. What a stack is. It has nothing to do with Stack class provided in C#.NET.

static void GetAddress()
{
unsafe
{
int i = 5;
object refer = new object();
// &i gets an address of variable 'i' and * operator gets the value
Console.WriteLine("Address of i:{0},{1}" , (uint)&i, *(&i));
Console.WriteLine("Address of refer:{0},{1}", (uint)(&i - 1), *(&i - 1));
}
}

Explanation
Here, i have defined two variables, named – i and refer . Both these variables sit on STACK, however the values stored by them are treated differently.
i stores the value directly [5 in our case as per statement], and
refer stores the address of an actual object allocated on a manged heap.
This is how Stack and Heap state would be after two initalizing statements.

So, if i have an access to an address of i and knowing that refer is on Stack just after i, decrementing 1 from i address [Pointer arithmetic], i’m now pointing to the refer which is the reference to the actual object (object()) sitting on heap.

You have a stack address and thus the value stored at that location. Using Pointer Arithmatic, do what you want WITH CARE.
MS should tag POINTERS with “HANDLE WITH UTMOST CARE” 🙂

Advertisement

5 thoughts on “Playaround with an address of object reference variable”

  1. It’s not quite accurate to say that & get the address of ‘i’ and * gets the value (of ‘i’ ?).
    While I can write &i, *i is invalid, because ‘i’ is not a pointer. The value of ‘i’ is just i.
    Writing *(&i) is redundant. *(&i) == i.
    It would have been less confusing to say that the * operator gets the value at a given address in memory, hence *(&i). * only works with addresses and pointers. Pointers only stores addresses.
    So if ‘i’ was a pointer I could write *i and get the value pointed by ‘i’, not the value of ‘i’. The value stored in ‘i’ would be an address in memory, and to get that value I would just write i.
    int x;
    int* i = &x;
    *i = 123; // initialize x
    Console.WriteLine(“Value of i is address of x? “, i == &x) // true
    Console.WriteLine(“Value of i: {0}”, (uint)i);
    Console.WriteLine(“Address of x: {0}”, &x);

    It could sound picky but when dealing with pointers, things should be said without any ambiguities.
    Anyway nice try for presenting pointers without overcomplicating the subject.

    Like

  2. Hi bro…..Nice post and i am having a doubt………Are we sure that all the time “refer” sits next to “i” on the stack…..I mean if we are executing 2 or more threads……

    Like

  3. Outstanding post, I conceive website owners should learn a lot from this web site its real user genial. So much superb information on here :D.

    Like

  4. Excellent beat ! I wish to apprentice whilst you amend your site, how can i subscribe for a blog web site? The account helped me a appropriate deal. I had been a little bit familiar of this your broadcast offered shiny clear idea.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s