0

I am implementing Linked List in C# so I use Two Approaches

First Approach

 unsafe public struct Node
        {
            public int info;
            public Node* link;
        }

Here Is Add Node Method

public void AddNode( int  a)
        {
            Node temp = new Node();
            temp.info = a;
               if (Head == null)
               {
                  Head = Current = &temp;        
               }
            else
            {
                    (*Current).link = &temp;  
                     Current = &temp; 
                }                                         
            }

When I run This Code It does not save memory means It create an instance of Node temp = new Node();Structure as long as it is inside AddNode Its memory is save but when I print It outside this method using Head its give me NullRefrenceException

Second Approach

By Using Class And Objects

    public class Node
        {
            public int info;
            public Node link;
        }
        class Linked_list
        {
           public Node Head;
           public Node Current;



            public void insert_element_linkedlist( int  a)
            {
                Node temp = new Node();
                temp.info = a;
                   if (Head == null)
                   {
                      Head = Current = temp;        
                   }
                else
                {
                        Current.link = temp;  
                         Current = temp; 
                    }                                         
                } 
}

The Second method is working perfectly means Every instance of temp node Node temp = new Node(); is save in memory .

Question : Why It does not save structure instance ?

Sorry For My Sub Standard English.

5
  • 2
    Is this simply an exercise to learn the language, or could you use the LinkedList class? Also, in C# using pointers is something you should only do in very weird circumstances; I don't think a linked list implementation requires it. Commented Dec 2, 2015 at 15:34
  • It is an Exercize of Data Structure Commented Dec 2, 2015 at 15:36
  • Why it saves an instance of a class but Does not save a structure instance What's This Commented Dec 2, 2015 at 15:37
  • I mentioned above that I am Implementing LinkedList not using Default LinkedList class Commented Dec 2, 2015 at 15:41
  • I believe the issue here is that you are using mutable structs, which don't behave the same way that classes do when you pass values around while mutating them. Commented Dec 2, 2015 at 15:43

1 Answer 1

1

The structure instance, being a value type, gets created on the stack, so when AddNode returns it is cleaned up and you are left with pointers to a random place in memory. Simply creating a pointer to a variable is not enough to keep it alive.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes Nice means structure is just like all other data types like int float
Objects are of reference type. For a struct, the data are actually stored on the stack; for an object, only the reference to the data is stored on the stack; the data are allocated on the heap. Objects don't go away until they are "unreachable" (no outstanding references to them from live objects or global variables), at which point they become eligible for garbage collection. Structs go away when the stack frame in which their data are stored goes away.
I should add that the above is probably only "approximately" correct; I may be using some wrong terminology, but that's the gist. Also, structs are not necessarily stored on the stack, as an object can contain a struct, which would then be part of the object's data on the heap. But their data are always stored "inline" rather than as a separate chunk of data pointed at by a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.