Re: Automatic Memory Management Question

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hi,

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

// The Stack class exists in the framework under System.Collections
// (and System.Collections.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@xxxxxxxxxxx> wrote in message news:1160568001.339645.68940@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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;
}
}
}



.



Relevant Pages

  • Re: You dont have to move or free pointers, fragmenting your heap.
    ... so I'm quite familiar with indexing into the array that is the stack. ... With a dynamic array of pointers ... Try doing that with linked lists! ...
    (microsoft.public.vc.language)
  • Re: Stack or Heap
    ... The combination of default low stack size ... combined with a default that puts many arrays on the stack ... programmers never use arrays prefering linked lists etc. ...
    (comp.lang.fortran)
  • You dont have to move or free pointers, fragmenting your heap.
    ... so I'm quite familiar with indexing into the array that is the stack. ... You don't have to move or free pointers, ... Try doing that with linked lists! ...
    (microsoft.public.vc.language)
  • Re: Lets put this to rest
    ... fundamental difference in design methodology. ... Deciding whether one needs stacks, linked lists, ... exceptions that depend on particular problem contexts that haven't been ... All the things in the *specification* for stack that you were given. ...
    (comp.object)
  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)