Heap and Stack

What is heap and stack? and what is their relationship?
I am new to this, and hope someone can recommend some introductory URLs that clearly introduce this, thanks:-)
LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

giltjrCommented:
Basically they are the same thing.  The difference is how they are used/accessed.  They both are an area of memory where you store something.  With a heap, you access what you are storing in any order you want and they stay there until you remove them.  

With a stack, you normally access them in the reverse order you put them on the stack and they are automatically removed as you access them.

http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci213047,00.html
http://whatis.techtarget.com/definition/0,,sid9_gci212239,00.html
william007Author Commented:
Hi, do you mean that the thing store in heap will stay there as long as it can be unless you remove them explicitly; and for the thing store stack will be remove automatically once you access it?
giltjrCommented:
Correct.

Another description from: http://blogs.wwwcoder.com/govinda/archive/2004/08/16/688.aspx

Stack and Heap:
Both of 'em are sources from which memory is allocated. Stack is automatic, which is created when memory is in scope, and destroyed when it is out of scope. Heap is manual and created and destroyed when requested. On stack, the local variables (unless they are static or register data members), function parameters are stored on stack. On heap normally global variables, static local variables, any data allocated by using calls like new, malloc, calloc etc. memory allocation calls.

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

InteractiveMindCommented:
William, if you're after an abstract definition of a stack & heap, then please ignore this post.. But if you have a particular context, then perhaps we can provide a simpler and more suitable definition ...

(e.g., the "Java Heap").
william007Author Commented:
Thanks for all the reply:-)

InteractiveMind,  I would like to hear about the definition in Java context.
InteractiveMindCommented:
The java heap is where the objects of a Java program live. Any time you allocate memory with the 'new' operator, that memory comes from the heap. The Java language doesn't allow you to free allocated memory directly. Instead, the runtime environment keeps track of the references to each object on the heap, and automatically frees the memory occupied by objects that are no longer referenced -- a process called garbage collection.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
william007Author Commented:
So, how about Java Stack? Is it only use for recursive call?
InteractiveMindCommented:
java.util.Stack
----------------
This class is a list delegate, which obeys a LIFO (Last In First Out) methodology.
It's a type which is available in most programming languages.

Java's Stack
--------------
This stack is produced and maintained by the JVM. It is generally used to keep track of recusive calls, and is useful for debugging errors (i.e., analysing the "Stack Trace").
william007Author Commented:
Hi, InteractiveMind,

Thanks, this gave me a more "concrete" view of heap and stack.
My last question is,
1)Is this practise apply to C++ and VB also?
2)How the Non Object Oriented language(like C etc) make use of heap?
InteractiveMindCommented:
I'm afraid that my understanding of lower-level programming is limited, so I could not give you a definite answer.
[Stack's as data types are certainly used in C, C++, and VB though]

Sorry
william007Author Commented:
Thanks:-)
giltjrCommented:
--> 1)Is this practise apply to C++ and VB also?

Assuming you are talking about stack processing, yes, all languages that make use of stacks are processed LIFO.


--> 2)How the Non Object Oriented language(like C etc) make use of heap?

These are used to store your variables or varaible that may be needed by the system.

See:

     http://www.cprogramming.com/tutorial.html

for more information.
william007Author Commented:
--> 1)Is this practise apply to C++ and VB also?
Are they using heap to store object, and static variable those kind of things also?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.