Here is a link to an article with a reasonable explanation of the
difference between stack and heap.  (Reasonable = better than the one
I will attempt to give)

http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91

In "old school" simple terms, the stack was where the progam
automatically allocated and deallocated memory for variables.  You
never directly managed the stack,  The heap was where you manually
allocated and deallocated memory as you needed it.  I say "Old School"
because managed environments make this distinction slippery and even
somewhat incorrect.

For example:

"Dim MyVar as Integer" allocates off the stack. When MyVar goes out of
scope the memory is immediately released and returned to the stack.
You don't use a  "New" command nor any other commands to allocate and
deallocate memory beyond stating the type of the variable.

Compare that to "Dim MyInstance as SomeClass" .   This declares a
variable of a type, but does not allocate any memory for the object
itself.  Right now it is, literally, Nothing and cannot be used in any
meaningful way.

When you assign a value to the MyInstance variable (MyInstance = New
SomeClass)  you are actually allocating a bliock of memory off the
heap and assigning to MyInstance a pointer to that memory.  (No,
methods and properties are not duplicated for each instance of the
classe.  They are kept in a seperate block of memory shared by all
instances of the same class.  Yes this requires complicated magic to
be performed at run-time.)

When the MyInstance variable goes out of scope, the memory is still
allocated.  In a non-mananged environment (C, C++, VB6, etc...) you
would be responsible for deallocating the memory and returning it to
the heap.  If you don't, it stays in memory for the life of the
program.  (Memory leaks, Ah! The bad old days. I remember them well.)
Since .Net is managed, eventually garbage collection kicks in and
releases the memory for you.  You can force memory to be released
(call Dispose), but if you forget to, don't worry, sooner or later the
memory will be cleaned up for you.  (In this way, .Net is like either
your mother or your wife, whichever applies.)

I could go on for quite a bit longer.  The way memory is managed on
the heap is quite complicated with lots of "gotchas" that made memory
leaks VERY common in non-managed environments.  .Net takes the
majority of this headache off the programmer's shoulders, making the
distinction between stack and heap less important than it once was.

Hope this helps.

-- RBDavidson

> Can you refresh my memory and explain the difference between the stack and
> heap?
>
>
>
>
>
>
>
> > Also classes have some code overhead that structs don't which results
> > in a struct instantiating faster than a class.  Not a big deal, unless
> > you  are doing something like
>
> > Dim myStruct(100000000) as SomeStruct
>
> > Vrs
>
> > dim myClass(100000000) as SomeClass
>
> > In which case every millisecond counts and structs have the
> > advantage.  Of course, since structs are value types you can't create
> > a new struct by inheriting from an existing one.  I.e. You can't
> > create an extensible "struct hierarchy" like you can with classes.
> > This limits their usefulness to pretty particular cases.
>
> > - RBDavidson
>
> > On Dec 15, 3:18 pm, "Brandon Betances" <[email protected]> wrote:
> > > Also, the only difference between a struct and a class is that a struct
> > is
> > > used for value types, not datatypes, System.Int32 for example is a
> > struct,
> > > string is a class.
>
> > > On Mon, Dec 15, 2008 at 4:11 PM, Brandon Betances <[email protected]
> > >wrote:
>
> > > > C# performs garbage collection automatically, but you can call it
> > > > expilictly with the System.GC.Collect() method.
> > > > put it where the delete statement is.
>
> >  > > On Mon, Dec 15, 2008 at 3:18 PM, Alon K <[email protected]> wrote:
>
> > > >> Hi,
>
> > > >> I remember that in C++ one had to issue the following statement (or
> > > >> something similar) to clean up memory:
>
> > > >> delete pNode;
>
> > > >> and the memory address assigned to pNode will be unassigned.
> > > >> Is there something like this that needs to be done in C# ?
>
> > > >> So for example I have:
>
> > > >> pHold = pIndex;
> > > >> pIndex = pIndex->pNext;
> > > >> delete pHold; ????? <-- what can I do here
>
> > > >> While on the topic, I have been doing the following to create linked
> > > >> lists in the intermediate step:
>
> > > >> NODE* pCreate = stackalloc NODE[1];
> > > >> pIndex->pNext = pCreate;
> > > >> pIndex = pCreate;
>
> > > >> While I haven't programmed in C++ in a while I remember that this
> > > >> could be done directly in C++:
>
> > > >> pIndex->(*pNext) = new NODE;
> > > >> pIndex = pIndex->pNext;
>
> > > >> Also the code above using stackalloc NODE[1] just feels wrong and as
> > > >> if I'm using it to do something it wasn't intended to do. Now for the
> > > >> application it is important to keep the spirit of a linked list and
> > > >> not an stacked list, meaning I want to be able to move the links
> > > >> around dynamically.
>
> > > >> How can I do this correctly, I may not be understanding the
> > > >> fundamentals of C# (such as how variables are treated). Also NODE is a
> > > >> struct, which is different from a Class in C# in terms of data from my
> > > >> understanding.
>
> > > >> Appreciate your help.
>
> >  > >> -Alon- Hide quoted text -
>
> > > - Show quoted text -
>
> --
> Charles A. Lopez
> [email protected]
>
> Bachelor of Arts - Computer Science
> New York University
>
> Registered Microsoft Partner
>
> New York City, NY- Hide quoted text -
>
> - Show quoted text -

Reply via email to