Automatic Memory Management Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gwainguard@hotmail.com

    #1

    Automatic Memory Management Question

    Hello I am studying the ECMA specs and was doing wonderfully until just
    now. They have just broached the topic of AMM and given some example
    code which seems to be demanding a greater understanding from the
    reader than the previous examples.

    I include the code following, would someone please be kind enough to
    comment it as thouroughly as possible so I can understand exactly how
    it is operating and what it is doing.

    I have used wikipedia to explore nodes and linked lists. And my
    understanding at the moment is that a node is a unit of data, and a
    linked list is a fundamental data structure.

    Most of this code goes over my head so please assume as little
    knowledge as possible.

    Many Thanks,
    GM.

    Code:

    public class Stack
    {
    private Node first = null ;
    public bool Empty {
    get {
    return (first == null);
    }
    }
    public object Pop() {
    if (first == null)
    throw new Exception("Can' t Pop from an empty Stack.");
    else {
    object temp = first.Value;
    first = first.Next;
    return temp;
    }
    }

    public void Push(object o) {
    first = new Node(o, first);
    }
    class Node
    {
    public Node Next;
    public object Value;
    public Node(object value): this(value, null) {}
    public Node(object value, Node next) {
    Next = next;
    Value = value;
    }
    }
    }

  • Dave Sexton

    #2
    Re: Automatic Memory Management Question

    Hi,

    Let me know if my comments aren't clear enough for you:

    // The Stack class exists in the framework under System.Collecti ons
    // (and System.Collecti ons.Generic in .NET 2.0) and provides a
    // FILO (first-in-last-out) list of elements.
    // I can't say for sure whether the managed stack is implemented as a
    // singly linked-list, such as in this example, but the behavior is the same.
    public class Stack
    {
    // first is a reference to the first Node in the linked list.
    // It is a linked list because Stack doesn't contain a direct reference to any other Node.
    // Stack has to use the link of the first Node to access the next Node, and so on.
    private Node first = null ;

    // Gets whether the Stack contains any nodes since the remainder of the link list
    // depends on whether first has been assigned to a non-null value.
    public bool Empty {
    get {
    return (first == null);
    }
    }

    // Removes and returns the object on the top of the stack.
    // The next object in the list, if one is available, is moved on top.
    public object Pop() {
    if (first == null)
    // Nothing to be popped (first Node doesn't exist)
    throw new Exception("Can' t Pop from an empty Stack.");
    else {
    // Grab the first Node
    object temp = first.Value;

    // assign the global variable, "first" to the next Node in the list
    // which is only accessible using the link from the current "first" Node.
    // Note, first.Next may very well be null, which indicates that the Stack
    // is now empty (see Empty property)
    first = first.Next;

    // return the previous first Node (popped)
    return temp;
    }
    }

    // Adds the specified object to the top of the Stack (first Node in the linked list)
    public void Push(object o) {
    // assign the global variable, "first" a reference to a new Node that represents
    // the specified object, making sure to link the current "first" Node to the
    // new Node via a constructor parameter. The link is important because it
    // defines the relationship between the new Node and the current "first" Node
    // within the linked list. The relationship can be viewed as parent --child
    // where the new "first" Node becomes the parent and the linked (previous) "first"
    // Node becomes the child.
    first = new Node(o, first);
    }

    // Nodes are used to encapsulate objects in the Stack's internal list. They provide
    // the linking capability on which the Stack relies. Nodes and objects
    // have a one-to-one correlation with each other. Nodes are used internally
    // by the Stack class. Actually, it makes sense for this class to either be nested in the
    // Stack class and marked private or to be left in-place and marked internal.
    class Node
    {
    // holds the link (reference) to the next Node in the Stack's internal list.
    // this field is the physical implementation of the linked list
    public Node Next;

    // holds a reference to the object that this Node encapsulates for the Stack
    // The consumer of the Stack class doesn't need to know about Nodes, just
    // the object references that are assigned to the Stack through the Push method
    public object Value;

    // Constructs a new instance of the Node class and does not link the Next node
    // This constructor overload is pointless, in this example (and probably in this pattern).
    public Node(object value): this(value, null) {}

    // Constructs a new instance of the Node class with the specified object value and
    // link (reference) to the next Node in the list. The presence of this constructor allows
    // the Stack class to create the linked-list.
    public Node(object value, Node next) {
    Next = next;
    Value = value;
    }
    }
    }

    --
    Dave Sexton

    <gwainguard@hot mail.comwrote in message news:1160568001 .339645.68940@c 28g2000cwb.goog legroups.com...
    Hello I am studying the ECMA specs and was doing wonderfully until just
    now. They have just broached the topic of AMM and given some example
    code which seems to be demanding a greater understanding from the
    reader than the previous examples.
    >
    I include the code following, would someone please be kind enough to
    comment it as thouroughly as possible so I can understand exactly how
    it is operating and what it is doing.
    >
    I have used wikipedia to explore nodes and linked lists. And my
    understanding at the moment is that a node is a unit of data, and a
    linked list is a fundamental data structure.
    >
    Most of this code goes over my head so please assume as little
    knowledge as possible.
    >
    Many Thanks,
    GM.
    >
    Code:
    >
    public class Stack
    {
    private Node first = null ;
    public bool Empty {
    get {
    return (first == null);
    }
    }
    public object Pop() {
    if (first == null)
    throw new Exception("Can' t Pop from an empty Stack.");
    else {
    object temp = first.Value;
    first = first.Next;
    return temp;
    }
    }
    >
    public void Push(object o) {
    first = new Node(o, first);
    }
    class Node
    {
    public Node Next;
    public object Value;
    public Node(object value): this(value, null) {}
    public Node(object value, Node next) {
    Next = next;
    Value = value;
    }
    }
    }
    >

    Comment

    • gwainguard@hotmail.com

      #3
      Thankyou

      Many thanks for your detailed response to my question.

      It has gone a long way to improving my understanding of what is
      happening here. I would say I am about 80% clear now whereas before I
      would have said i was about 5% clear.

      Many thanks for elucidating the meanings here Mr. Sexton,

      GM.

      Comment

      • Dave Sexton

        #4
        Re: Automatic Memory Management Question

        Hi,

        Glad to help.

        Would you like us to take a shot at that last 20%?

        --
        Dave Sexton

        <gwainguard@hot mail.comwrote in message news:1160574911 .605891.202210@ h48g2000cwc.goo glegroups.com.. .
        Many thanks for your detailed response to my question.
        >
        It has gone a long way to improving my understanding of what is
        happening here. I would say I am about 80% clear now whereas before I
        would have said i was about 5% clear.
        >
        Many thanks for elucidating the meanings here Mr. Sexton,
        >
        GM.
        >

        Comment

        Working...