Question

I am trying to print the address of a particular node in c#.

Here is the function which finds the minimum two elements(suppose min1 and min2) in the existing linked list. For some reasons i want to know the address of the first minimum occurred in the list(that may be min1 or min2, irrespective of which one is greater or which one is smaller, I just need to know the address of the first minimum found on traversal from head of the Linked List to NULL).

I tried to do so and my code is able to find the minimum two and also i tried to find the address of the first minimum element arrived on traversing from head in a while loop which breaks and store the address in address variable and come out of the loop.

But the problem is instead of printing the value of address it prints a long string as given below:

shekhar_final_version_Csharp.Huffman+Node  

where shekhar_final_version_Csharp.Huffman is the name of namespace.

Below is code :

public Node find_two_smallest(ref Node pmin1, ref Node pmin2) 
{
    Node temp = tree;
    Node address = tree;
    Node min1 ;
    min1 = new Node();
    min1.freq = int.MaxValue;
    Node min2;
    min2 = new Node();
    min2.freq = int.MaxValue;
    while (temp != null) 
    {
        if (temp.is_processed == 0) 
        {
            if (temp.freq < min2.freq) 
            {
                min1 = min2;
                min2 = temp;
            } 
            else if (temp.freq < min1.freq && temp.freq!=min2.freq) 
            {
                min1 = temp;
            }
            temp = temp.next;
        }
    }
    pmin1 = min1;
    pmin2 = min2;

    // Below is the code to find the address of first minimum arriving on traversal
    // of List.
    while (temp != null) 
    {
        if (temp.freq == min2.freq || temp.freq == min1.freq ) 
        {
            address=temp;
            break;
        } 

     }          
    // See below the output corresponding to it.
    Console.WriteLine("Address check: {0} Its value is : {1}", 
                      address, address.freq);
    return address;
}

It's corresponding output is:

Address check: shekhar_final_version_Csharp.Huffman+Node Its value is : 88 

where it is showing correctly the value at that location (min1/min2=88) but not showing address, I want to see the integer value of the address.

Could some one please help me in achieving my target ?hanks.

When i try to see the address using & operaor it gives following error.

hp@ubuntu:~/Desktop/Internship_Xav/c#$ gmcs check1.cs /unsafe
check1.cs(119,76): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
check1.cs(119,76): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type `shekhar_final_version_Csharp.Huffman.Node'
check1.cs(12,22): (Location of the symbol related to previous error)
Compilation failed: 2 error(s), 0 warnings
Was it helpful?

Solution

You cannot get the memory address of a managed object like this. It is simply not a supported feature of the language.

OTHER TIPS

Console.WriteLine calls ToString() on the objects passed to it and then prints this string. The default behavior of ToString() is to print the type name. If you want something else, override it in your classes:

public class Node
{
    ...

    public override string ToString()
    {
        return Value; // Or whatever properties you want to print.
    }
}

Note: In C# you cannot get the memory address of objects. This is because the garbage collector (GC) can relocate objects when collecting. If you need to identify an object, add your own identifier as a field or property. For instance with a Guid field:

public class Node
{
    private Guid _guid = Guid.NewGuid();

    public override string ToString()
    {
        return _guid;
    }
}

Or by using a static counter:

public class Node
{
    private static int _counter = 0;

    private int _id = _counter++;

    public override string ToString()
    {
        return _id;
    }
}

In c# to refer to the address of a refererence type use the '&' operator

Console.WriteLine("Address check: {0} Its value is : {1}", &address, address.freq);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top