Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Heap and Stack
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.


Paul
Hi,

I am currently learning C# and have a question on the
importance of the heap and the stack. My understanding is
that these are both memory but each have a different
memory management system. I understand that reference
types are stored on the heap and data types are generally
stored on the stack (ignoring boxing etc). What I don't
understand is what the importance of this is ?

Can someone explain why the use of the heap and the stack
is so important and what the exact differences between
both are.

Could someone also give me a real world example of where
you would use static members and polymorphisim.

Thanks

Paul

Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Jonathan Allen
[Original message clipped]

There are many stacks and heaps in programming, but I think this is the one you need.

The Stack is where temporary values go. When you call a function, N Bytes are grabbed from the top of the Stack, N being the total size of all the local variables. The area a particular function has staked out as its own is called its Stack Frame. Since each function can call another function, you end of with a series of Stack Frames, each one on top of another. When a function exits, its Stack Frame is destroyed. Anything in it is thus lost.

A Stack Overflow occurs when you run out of space in the Stack.Generally speaking, this happens when a poorly written function calls itself an infinite number of times. That results in an infinite number of stack frames, which needs N times infinity bytes.

The Heap is basically a bottomless pool of memory. Whenever you need a chunk of memory for an indefinite amount of time, you get it from the Heap. The Heap in C# may look more like a stack to some, but for tradition's sake we will still call it a Heap.

The Reference Type, also known as an Object, is always stored in the Heap. There is no way to put one in the stack. A reference or pointer to it can be stored in the Stack or in another Object. It exists as long as there is at least one reference pointing to it.

The Value Type, also known as a Structure, can be stored in the Stack or in an Object. If it is in the Stack, it only exists as long as that Stack Frame exists.

Value Types can be "Boxed". This means the program is pretending that it is a Reference Type and puts it in the Heap.

[Original message clipped]

Static Members are used in two ways. The first is to create function libraries, like System.Math. There is no Math object, one just uses the functions directly. The second is to add support to a class, such as Double.Parse. In this case, Parse doesn't actually work on a Double, but rather returns one.

Polymorphism is used to create various different versions of the same basic thing. For instance, there are numerous Stream classes. Each one works very differently, often operating on things like Files or Sockets, but they can all be used by something that just needs a Stream. Another example is the Formatters used in serialization. Whenever something needs a formatter, you can give it either a BinaryFormatter or a SoapFormatter as you see fit.

Polymorphism also works on interfaces. The For-Each construct works on any class that implements IEnumerable. This includes a wide variety of Arrays and Collections. Other interfaces used a lot in polymorphism include ICollection and IList.

--
Jonathan Allen

"Paul" <Click here to reveal e-mail address> wrote in message news:2b3201c15a75$b90572a0$3def2ecf@TKMSFTNGXA14...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
Stan Shankman
Jonathan,

Thank you for your write-up on Stacks and Heaps.
I think there is a real need for more discussion along these lines in the
early chapters of books devoted to C#. (Are you authors out there
listening?) In particular, one would hope to learn more about the "software
engine" (i.e. the CLR) before delving too deeply into the language. Some
simple diagrammatic representation of the whole scheme would be desirable.
Along with, perhaps, a simple example of how real-world code makes its way
through the "engine".

So I'm sure that many readers out there are appreciative of your thoughtful
reply.

Just my 2¢ worth,
Stan Shankman

"Jonathan Allen" <Click here to reveal e-mail address> wrote in message
news:Oni7GuqWBHA.1396@tkmsftngp07...
[Original message clipped]

There are many stacks and heaps in programming, but I think this is the one
you need.

The Stack is where temporary values go. When you call a function, N Bytes
are grabbed from the top of the Stack, N being the total size of all the
local variables. The area a particular function has staked out as its own is
called its Stack Frame. Since each function can call another function, you
end of with a series of Stack Frames, each one on top of another. When a
function exits, its Stack Frame is destroyed. Anything in it is thus lost.

A Stack Overflow occurs when you run out of space in the Stack.Generally
speaking, this happens when a poorly written function calls itself an
infinite number of times. That results in an infinite number of stack
frames, which needs N times infinity bytes.

The Heap is basically a bottomless pool of memory. Whenever you need a chunk
of memory for an indefinite amount of time, you get it from the Heap. The
Heap in C# may look more like a stack to some, but for tradition's sake we
will still call it a Heap.

The Reference Type, also known as an Object, is always stored in the Heap.
There is no way to put one in the stack. A reference or pointer to it can be
stored in the Stack or in another Object. It exists as long as there is at
least one reference pointing to it.

The Value Type, also known as a Structure, can be stored in the Stack or in
an Object. If it is in the Stack, it only exists as long as that Stack Frame
exists.

Value Types can be "Boxed". This means the program is pretending that it is
a Reference Type and puts it in the Heap.

[Original message clipped]

Static Members are used in two ways. The first is to create function
libraries, like System.Math. There is no Math object, one just uses the
functions directly. The second is to add support to a class, such as
Double.Parse. In this case, Parse doesn't actually work on a Double, but
rather returns one.

Polymorphism is used to create various different versions of the same basic
thing. For instance, there are numerous Stream classes. Each one works very
differently, often operating on things like Files or Sockets, but they can
all be used by something that just needs a Stream. Another example is the
Formatters used in serialization. Whenever something needs a formatter, you
can give it either a BinaryFormatter or a SoapFormatter as you see fit.

Polymorphism also works on interfaces. The For-Each construct works on any
class that implements IEnumerable. This includes a wide variety of Arrays
and Collections. Other interfaces used a lot in polymorphism include
ICollection and IList.

--
Jonathan Allen

"Paul" <Click here to reveal e-mail address> wrote in message
news:2b3201c15a75$b90572a0$3def2ecf@TKMSFTNGXA14...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Jonathan Allen
[Original message clipped]

Sometimes I wish I was an author instead of just a technical editor so I
could address stuff like that. But then I remember how much hell I put the
authors through and become content to be on the safe side of the pitchfork.

--
Jonathan Allen

"Stan Shankman" <Click here to reveal e-mail address> wrote in message
news:#9cQIa0WBHA.2244@tkmsftngp03...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
 
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IList
System.Collections.Stack
System.Double
System.Math
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.Runtime.Serialization.Formatters.Soap.SoapFormatter




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734