473,657 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}
}
}

Oct 11 '06 #1
3 1459
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

<gw********@hot mail.comwrote in message news:11******** *************@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;
}
}
}

Oct 11 '06 #2
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.

Oct 11 '06 #3
Hi,

Glad to help.

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

--
Dave Sexton

<gw********@hot mail.comwrote in message news:11******** **************@ 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.

Oct 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3341
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all images that have been used and are going to be closed. This is how ever no way to reduce the memory consumption. I have noticed , using the task manager, that garbage collector doesn't actually do any collections unless the computer becomes low...
9
2013
by: Rafael Charnovscki | last post by:
I can comprehend the basics of that subject (pointers, memory allocation, heap, stack etc), but I am interested to have references with more details. I have some C/C++ books and lots of URLs "bookmarked", but all of them don't go too far on memory management as I would like. One of my main issues is whether function "main" and its local variables are allocated on the stack area as other functions. Does anybody recommend a good book or...
1
2794
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots of memory. Suppose i run first C# application which has occupied all memory and
13
1850
by: Vijay | last post by:
Hi All, I am learning C++ and have one question. Using free or delete we can release the memory. After releasing memory where this released memory will go.. Does it go to back operating system? Please explain. Thanks in advance.
3
5306
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
9
2340
by: benoit808 | last post by:
I don't have a lot of experience with C++ so I apologize if this is a stupid question. I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory leak but I don't think there's one. Here is the code (i took some stuff out to simplify): void myFunction() { Sprite *pImage; sprintf(pStr, "s04-%02d.png", i);
58
4643
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
3
6308
by: Laurence | last post by:
Hi folks, DB2 UDB supports automatic storage management in v8.2.2 and v9. The question is how do I know the databases and/or tablespaces are enable "automatic storage management" or not after created for a period of time? Thanks in advance,
5
24702
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
25
2646
by: sidd | last post by:
In the following code: int i = 5; ---it goes to .data segment int j; ---it goes to bss segment int main() { int c; int i = 5; ---stack
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.