473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

newbee

why do you need a heap and a stack

why not all memory called a heap or call it a stack what is the purpose of
having a heap and a stack

Nov 16 '05 #1
3 1693
This is more of a general compiler design question than a C# question.
Suggest you use Google and search on something like:

stack heap tutorial

.... you'll get lots of explanation.

The short answer, though, is that this is universal in all languages ... the
stack is for local variables and provides very fast access via push and pop.
The heap is for larger and/or more complex structures. The heap is slower
to access and more difficult to manage, but is ideal for arbitrary hunks of
data. In C#, reference types (object instance data) is on the heap; value
types are on the stack.

--Bob

"nahur" <na***@hotmail.com> wrote in message
news:%2*****************@TK2MSFTNGP15.phx.gbl...
why do you need a heap and a stack

why not all memory called a heap or call it a stack what is the purpose of
having a heap and a stack

Nov 16 '05 #2
This is a post I saved a while ago -

"The heap and the stack are the two places where programs store data (okay,
there are a few more, but they're the two most common ones).

The heap is just a big chunk of memory that is managed by a heap manager (or
garbage collector, in C#'s case). When you write something like:

Employee e = new Employee();

(assuming Employee is a class), you get a chunk of memory allocated out of
the heap to store the information for that employee instance. This instance
will be preserved until it isn't needed anymore (in C#), or until it is
explicitly deleted (in languages like C++).

The stack is used to hold local variables. So, when I write something like:

int i = 15;

as part of a method, that sets aside a chunk of space on the stack to hold
the value "15". When I enter a method, that space is used on the stack, and
when I leave the method, that space is reclaimed for later use.

To be strictly true, in my first example, I not only allocated an instance
of Employee on the heap, I allocated a reference to that on the stack.

Or, to be shorter, all local variables use up space on the stack. If they
are value types, this space is used to store the actual value. If they are
references types, this space is used as a reference (aka pointer) to the
actual value, which is stored on the heap.
--
Eric Gunnerson"
Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html

"nahur" <na***@hotmail.com> wrote in message
news:##*************@TK2MSFTNGP15.phx.gbl...
why do you need a heap and a stack

why not all memory called a heap or call it a stack what is the purpose of
having a heap and a stack

Nov 16 '05 #3
> why not all memory called a heap or call it a stack what is the purpose of
having a heap and a stack


in addition to the information given by the others:

the heap is a virtually unlimited storage of memory in which all objects are
created. there are a huge amount of occupied blocks of memory separated with
free holes. when a new object is created, a free block of memory has to be
found that is large enough to store the object's data.

the stack is a logically separated fragment of memory (physically it can be
anywhere, even in the middle of the heap, although usually it is somewhere
else). when a method calls another method, it pushes all parameters to that
method on the stack as well as the return address. after the new method is
about to finish, it pops the return address from the stack.

so, the main purpose of the stack is to control the control flow between
method calls (it also speeds the use of method's local variables but it is
not the main purpose).

but why do we need the stack at all?

suppose there's no stack but the heap only. there would be no problem with
passing parameters to consecutive method calls but you would have to SEARCH
the heap for the free block of memory to store the parameters there. this
searching could be SLOW (managing the heap is a bit difficult).

that's why a separated fragment of memory (stack) is used to pass parameters
between methods: you do not have to search for a free block of memory on the
stack, because stack grows and shrinks linearly.

regards,
Wiktor Zychla
Nov 16 '05 #4

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

Similar topics

8
by: Gene Kahn | last post by:
Windows XP, Apache 2, PHP5 RC1 Hello, A total newbie here. I can't seem to get Apache2 to load the php5 module. When I loaded up this html file in a browser, only the Today's date: line prints on...
3
by: Newbee | last post by:
Hi ! Let's say that this is the folder on the server: /web/firstDir/secondDir/images/image.gif where i have stored my pictures. I have tryed with apsolute and relative paths but i can't display...
2
by: Newbee Adam | last post by:
some said that .NET app can run on any program where rutime exists. What is "runtime" in this sense? will I have to install runtime or .net framework or .NET support on an xp machine for a...
4
by: PerryC | last post by:
All, 1. Do the following codes seem ok? 2. If so, then how do I pull the value of YOE1 and YOE2 into my report? (to do some further calculations) ...
2
by: Martin Hvidberg | last post by:
Dear list I have found a declaration like this: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include "ectemp.h"
9
by: EMW | last post by:
I have created a page in aspx and after a click on a button, a new page should open. How is this possible? I tried it doing it like in vb.NET with opening a new form, but it doesn't work. rg,...
8
by: ikarias | last post by:
Maybe not the right place to aske this, but a newbee (me) needs help. i trying to input a number into a textbox, so i can make a series of calculations. in the old days of basic i just jused...
2
by: Mel | last post by:
I have a selection box with 3 values. what i need is to pass 3 urls to a function that has a switch statement and based on the selection index goes on one of the tree urls. Question is how do i...
0
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I have a couple of newbee questions. I have a .aspx page with a couple of TextBoxes and a submit button. Now... When i press the submitbutton i want the data in the TextBoxes to be...
2
by: Rene | last post by:
Hello to all! I am a newbee to C++, I did a beginners' course, and now I am stuck with a strange problem. I have written the function that is pasted downwards. The peculiar thing is that when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...

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.