1

I have a question on how a particular line of code works in regards to memory allocation. I am referring a piece of code to convert array list into linkedlist.

Attaching the code below

// C# implementation of the above approach 
using System; 
          
class GFG
{ 
    // Representation of a node
    public class Node 
    {
        public int data;
        public Node next;
    }
      
    // Function to insert node
    static Node insert(Node root, int item)
    {
        Node temp = new Node();
        Node ptr;
        temp.data = item;
        temp.next = null;
        if (root == null)
        {
            root = temp;
        }
        else
        {
            ptr = root;
            while (ptr.next != null)
            {
                ptr = ptr.next;
            }
    ======> ptr.next = temp;
        }
        return root;
    }
      
    static void display(Node root)
    {
        while (root != null) 
        {
            Console.Write(root.data + " ");
            root = root.next;
        }
    }
      
    static Node arrayToList(int []arr, int n)
    {
        Node root = null;
        for (int i = 0; i < n; i++)
            root = insert(root, arr[i]);
        return root;
    }
      
    // Driver code
    public static void Main(String []args)
    {
        int []arr = { 1, 2, 3, 4, 5 };
        int n = arr.Length;
        Node root = arrayToList(arr, n);
        display(root);
    }
}

So my doubt is once we assign an object Ptr to root then whatever change we make to Ptr will be reflected in root since both point to same memory. In 3rd iteration Ptr =Ptr.next, so it is referring to new memory location and when you inspect element Ptr it is assigned 2nd child node. So how come root object gets modified by ptr=ptr.next with different memory address. That is my question.

Thanks

Frederik Hoeft
  • 1,177
  • 1
  • 13
  • 37
vjalex
  • 63
  • 2
  • 13
  • 1
    You are **not** using pointers (and you shouldn't). What you call "pointers" here are references. References are not meant to be pointers, and how they are implemented is up to the .NET runtime (CLR). Since live objects residing in the heap can be moved around in the memory by the garbage collector, a reference has not the same semantics as a pointer (as a reference refers to a particular object instance, regardless when and how it is moved around by the garbage collector). Basically, you are doing yourself a disservice and confuse yourself if you equate references with pointers. –  Oct 21 '22 at 09:54
  • What do **exactly** do you mean by saying "_So how come root object gets modified by ptr=ptr.next_"? **Edit** your question and clarify this nebulous statement. `ptr=ptr.next` itself is not modifying the root object. It simply assigns the reference value hold by _ptr.next_ to the _ptr_ variable. That's all it does, nothing more. –  Oct 21 '22 at 09:59
  • Can you elaborate more. So what you are telling is root first get assigned to Ptr and then in the line marked by arrow it gets assigned to child nodes then again after coming out of else condition it again points to root? – vjalex Oct 21 '22 at 10:04
  • Sorry if it doesnt make any sense, my first language isnt english. Basically can you explain how root object gets modified in the loop when ptr is assigned to ptr.next – vjalex Oct 21 '22 at 10:06
  • There is nothing more to elaborate. `ptr=ptr.next` simply assigns the reference value hold by ptr.next to the ptr variable. As i said, that's all it does, nothing more. There is nothing more to elaborate about that. I cannot explain why you think or see your root object getting modified. I won't debug your code and try to make sense of it. [Debugging is something you should be able to do yourself.](https://learn.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger) –  Oct 21 '22 at 10:09
  • So ptr =per.next that I understood... how come root obj 's next get assigned the ptr.next. I have debugged the code I want to know the underlying process on how objects gets assigned. Also last I checked aren't references also kind of like pointers.... – vjalex Oct 21 '22 at 14:06
  • "_Also last I checked aren't references also kind of like pointers_" Well, that interpretation depends how far you are willing to stretch the meaning of the word "kind". References are not pointers, because their purpose is not to point at memory addresses. Hence why references are _not_ called pointers. If references were pointers, then why are they called "references" instead of "pointers"? "_how come root obj 's next get assigned the ptr.next._" Does it? (I am not certain, but i have only your word to rely on). If it does, then something in your code assigned it to it. Simple as that... –  Oct 21 '22 at 14:28
  • Sir, this is a code from a site I am referring. I debugged this code and found that indeed the root obj is getting modified before returning from the method. All I want to know is how it does that. This is the whole code I have not added anything to it. – vjalex Oct 21 '22 at 14:33

0 Answers0