473,396 Members | 1,891 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.

stack and a heap

what is the difference between a heap and a stack?
Nov 17 '05 #1
9 3334
They are both memory which is used to store objects. The stack by default
only stores Value types like Struct and Integers, whereas the Heap is used
to allocate memory for reference types.

There are numerous articles on the net about this. Try googling.
--
Terry Burns
http://TrainingOn.net

"shine" <sh***@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
what is the difference between a heap and a stack?

Nov 17 '05 #2
Terry Burns <he**@now.com> wrote:
They are both memory which is used to store objects. The stack by default
only stores Value types like Struct and Integers, whereas the Heap is used
to allocate memory for reference types.


That's an oversimplification, unfortunately. The stack is also used to
store references (rather than the objects themselves), and all types
can end up on the heap as part of other objects.

See http://www.pobox.com/~skeet/csharp/memory.html

Note that this doesn't explain the difference between the heap and the
stack, just what goes where.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

"shine" <sh***@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
what is the difference between a heap and a stack?


A stack is a chunk of process memory, that's being used to store local
variables, function return addresses, pointers to next stack frame and
things like that. The stack never holds instances of reference types, when a
local variable refers to a reference type, then the value of the variable
holds a reference to a GC heap allocated object instance, however, when a
local variable refers to a value type, then the value of the type is stored
inline with that variable. Each thread in the process has his own private
stack space, allocated at thread creation time with a fixed maximum size
(default 1MB).
A "StackOverflow" exception will get thrown whenever the stack gets full.
A heap is chunk of process memory that's being created/managed by the OS on
request of the executing program code. The managed heap or GC heap is just
another heap that's being created/managed by the OS on request of the CLR.
The GC heap is a private store managed by the CLR and only used to store
managed objects (instances of reference types or boxed value types). The
size of the GC heap is variable, and is only restricted by the amount of
free virtual address space in the process. A "MemoryOverflow" gets thrown
whenever the available virtual address space is exhausted.

Willy.

Nov 17 '05 #4
Yes, I know, sometimes when you know something implicitly, you forget to
state it, thanks for correcting my text.

--
Terry Burns
http://TrainingOn.net
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Terry Burns <he**@now.com> wrote:
They are both memory which is used to store objects. The stack by default
only stores Value types like Struct and Integers, whereas the Heap is
used
to allocate memory for reference types.


That's an oversimplification, unfortunately. The stack is also used to
store references (rather than the objects themselves), and all types
can end up on the heap as part of other objects.

See http://www.pobox.com/~skeet/csharp/memory.html

Note that this doesn't explain the difference between the heap and the
stack, just what goes where.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
When an application is opened Windows allocates the full 32 bit address
space (4 gigs) of memory. (this is theoretical and Windows memory manager
maps from the actual memory to the 32 bit memory space) It sub-divides this
into two parts, the Stack and Heap. The stack holds the declared named
varibles. In the case of a primative data type like 'byte' it holds the
value,

byte mybyte;
mybyte = 125;

In the case of instantiating a class, the declared name varible is writen to
the stack. The instaniated Object in writen to the Heap. The value of the
varible in the stack holds the address space of the Object in the Heap.

sqlconnection myconnection; (declares the varible myconnection of type
sqlconnection in the stack. If you tried to use myconnection
now you would get a null
reference error because there isn't a memory address in the value yet.)

myconnection = new sqlconnection; (this line instantiates the class
'sqlconnection' into an Object in the Heap, and enters the address
of the Object in
the value for myconnection. myconnection becomes the pointer that references
the Object
'sqlconnection' in the Heap.)
"shine" <sh***@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
what is the difference between a heap and a stack?

Nov 17 '05 #6

"Gino Cerone" <co****@covad.net> wrote in message
news:u%***************@TK2MSFTNGP09.phx.gbl...
When an application is opened Windows allocates the full 32 bit address
space (4 gigs) of memory. (this is theoretical and Windows memory manager
maps from the actual memory to the 32 bit memory space) It sub-divides
this
into two parts, the Stack and Heap. The stack holds the declared named
varibles. In the case of a primative data type like 'byte' it holds the
value,

byte mybyte;
mybyte = 125;

In the case of instantiating a class, the declared name varible is writen
to
the stack. The instaniated Object in writen to the Heap. The value of the
varible in the stack holds the address space of the Object in the Heap.

sqlconnection myconnection; (declares the varible myconnection of type
sqlconnection in the stack. If you tried to use myconnection
now you would get a null
reference error because there isn't a memory address in the value yet.)

myconnection = new sqlconnection; (this line instantiates the class
'sqlconnection' into an Object in the Heap, and enters the address
of the Object in
the value for myconnection. myconnection becomes the pointer that
references
the Object
'sqlconnection' in the Heap.)
"shine" <sh***@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
what is the difference between a heap and a stack?


Sorry, but you present a very simplistic view of the real process memory
space and you got it wrong on several points.
1. The full 32 bit address space is not allocated as user process space,
only a maximum of 2GB or 3GB (using /3GB RAM tuning switch in boot.ini) can
ever be reserved (and possibly committed).
2. This space is not sub-divided into a stack and a heap, there is stack
space allocated per thread created and there is heap space. When the OS
creates a process it always starts with a single thread so your process
always start with a single stack (default size: 4KB committed, 1MB reserved
space).
The default sizes of heap and stack space is determined by:
- the built process - managed code generators generate defaults (see later),
using unmanaged code build tools (like link.exe) one can specify other
values for both heap and stack.
- the values assigned by tools like editbin....
The default sizes for ".NET" processes is:
for the heap 4KB committed and 1MB reserved, extra space can (and will)
be requested by the process at run time.
for the stack 4KB committed and 1MB reserved, the stack can never grow
beyond the reserved maximum.

3. The stack only holds local variables, not all "the declared named
variables".
4. Primitive types (as all value types) are only stack allocated when
declared local (they have function scope), instance variables are GC heap
allocated. Note that locals can also be register allocated!

5. This: sqlconnection myconnection; (declares the varible myconnection of type
sqlconnection in the stack. If you tried to use myconnection
now you would get a null
reference error because there isn't a memory address in the value yet.)

is not completely true, the compiler will not allow you to use an
uninitialized local variable. If he would allow it, the variable could hold
any possible value that was left on the stack at the location of the
variable.

Willy.
Nov 17 '05 #7
The information that I wrote was abstracted from this link.
http://www.softec.uni-duisburg-essen...s%20NET-Framew
ork%20(v/Document%20Library/CSharp_06_MemoryManagement.pdf
Which was writen by Dr. Stefan Eicker, who holds a PHD in computer science,
and is a University professor and head of software engineering at the
University of Essen Germany. So it is ( according to you) Dr. Stefan Eicker
who is simplistic and wrong.
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:#9*************@TK2MSFTNGP10.phx.gbl...

"Gino Cerone" <co****@covad.net> wrote in message
news:u%***************@TK2MSFTNGP09.phx.gbl...
When an application is opened Windows allocates the full 32 bit address
space (4 gigs) of memory. (this is theoretical and Windows memory manager maps from the actual memory to the 32 bit memory space) It sub-divides
this
into two parts, the Stack and Heap. The stack holds the declared named
varibles. In the case of a primative data type like 'byte' it holds the
value,

byte mybyte;
mybyte = 125;

In the case of instantiating a class, the declared name varible is writen to
the stack. The instaniated Object in writen to the Heap. The value of the varible in the stack holds the address space of the Object in the Heap.

sqlconnection myconnection; (declares the varible myconnection of type sqlconnection in the stack. If you tried to use myconnection
now you would get a null
reference error because there isn't a memory address in the value yet.)

myconnection = new sqlconnection; (this line instantiates the class
'sqlconnection' into an Object in the Heap, and enters the address
of the Object in
the value for myconnection. myconnection becomes the pointer that
references
the Object
'sqlconnection' in the Heap.)
"shine" <sh***@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
what is the difference between a heap and a stack?


Sorry, but you present a very simplistic view of the real process memory
space and you got it wrong on several points.
1. The full 32 bit address space is not allocated as user process space,
only a maximum of 2GB or 3GB (using /3GB RAM tuning switch in boot.ini)

can ever be reserved (and possibly committed).
2. This space is not sub-divided into a stack and a heap, there is stack
space allocated per thread created and there is heap space. When the OS
creates a process it always starts with a single thread so your process
always start with a single stack (default size: 4KB committed, 1MB reserved space).
The default sizes of heap and stack space is determined by:
- the built process - managed code generators generate defaults (see later), using unmanaged code build tools (like link.exe) one can specify other
values for both heap and stack.
- the values assigned by tools like editbin....
The default sizes for ".NET" processes is:
for the heap 4KB committed and 1MB reserved, extra space can (and will) be requested by the process at run time.
for the stack 4KB committed and 1MB reserved, the stack can never grow
beyond the reserved maximum.

3. The stack only holds local variables, not all "the declared named
variables".
4. Primitive types (as all value types) are only stack allocated when
declared local (they have function scope), instance variables are GC heap
allocated. Note that locals can also be register allocated!

5. This:
sqlconnection myconnection; (declares the varible myconnection of type sqlconnection in the stack. If you tried to use myconnection
now you would get a null reference error because there isn't a memory address in the value yet.) is not completely true, the compiler will not allow you to use an
uninitialized local variable. If he would allow it, the variable could

hold any possible value that was left on the stack at the location of the
variable.

Willy.

Nov 17 '05 #8
This takes me back to the playground

8=}

--
Terry Burns
http://TrainingOn.net


"Gino Cerone" <co****@covad.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
The information that I wrote was abstracted from this link.
http://www.softec.uni-duisburg-essen...s%20NET-Framew
ork%20(v/Document%20Library/CSharp_06_MemoryManagement.pdf
Which was writen by Dr. Stefan Eicker, who holds a PHD in computer
science,
and is a University professor and head of software engineering at the
University of Essen Germany. So it is ( according to you) Dr. Stefan
Eicker
who is simplistic and wrong.
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:#9*************@TK2MSFTNGP10.phx.gbl...

"Gino Cerone" <co****@covad.net> wrote in message
news:u%***************@TK2MSFTNGP09.phx.gbl...
> When an application is opened Windows allocates the full 32 bit address
> space (4 gigs) of memory. (this is theoretical and Windows memory manager > maps from the actual memory to the 32 bit memory space) It sub-divides
> this
> into two parts, the Stack and Heap. The stack holds the declared named
> varibles. In the case of a primative data type like 'byte' it holds the
> value,
>
> byte mybyte;
> mybyte = 125;
>
> In the case of instantiating a class, the declared name varible is writen > to
> the stack. The instaniated Object in writen to the Heap. The value of the > varible in the stack holds the address space of the Object in the Heap.
>
> sqlconnection myconnection; (declares the varible myconnection of type > sqlconnection in the stack. If you tried to use myconnection
> now you would get a null
> reference error because there isn't a memory address in the value yet.)
>
> myconnection = new sqlconnection; (this line instantiates the class
> 'sqlconnection' into an Object in the Heap, and enters the address
> of the Object
> in
> the value for myconnection. myconnection becomes the pointer that
> references
> the Object
> 'sqlconnection' in the Heap.)
>
>
> "shine" <sh***@discussions.microsoft.com> wrote in message
> news:5D**********************************@microsof t.com...
>> what is the difference between a heap and a stack?
>
>


Sorry, but you present a very simplistic view of the real process memory
space and you got it wrong on several points.
1. The full 32 bit address space is not allocated as user process space,
only a maximum of 2GB or 3GB (using /3GB RAM tuning switch in boot.ini)

can
ever be reserved (and possibly committed).
2. This space is not sub-divided into a stack and a heap, there is stack
space allocated per thread created and there is heap space. When the OS
creates a process it always starts with a single thread so your process
always start with a single stack (default size: 4KB committed, 1MB

reserved
space).
The default sizes of heap and stack space is determined by:
- the built process - managed code generators generate defaults (see

later),
using unmanaged code build tools (like link.exe) one can specify other
values for both heap and stack.
- the values assigned by tools like editbin....
The default sizes for ".NET" processes is:
for the heap 4KB committed and 1MB reserved, extra space can (and

will)
be requested by the process at run time.
for the stack 4KB committed and 1MB reserved, the stack can never
grow
beyond the reserved maximum.

3. The stack only holds local variables, not all "the declared named
variables".
4. Primitive types (as all value types) are only stack allocated when
declared local (they have function scope), instance variables are GC heap
allocated. Note that locals can also be register allocated!

5. This:
> sqlconnection myconnection; (declares the varible myconnection of type > sqlconnection in the stack. If you tried to use myconnection
> now you would get a null > reference error because there isn't a memory address in the value yet.)

is not completely true, the compiler will not allow you to use an
uninitialized local variable. If he would allow it, the variable could

hold
any possible value that was left on the stack at the location of the
variable.

Willy.


Nov 17 '05 #9

"Gino Cerone" <co****@covad.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
The information that I wrote was abstracted from this link.
http://www.softec.uni-duisburg-essen...s%20NET-Framew
ork%20(v/Document%20Library/CSharp_06_MemoryManagement.pdf
Which was writen by Dr. Stefan Eicker, who holds a PHD in computer
science,
and is a University professor and head of software engineering at the
University of Essen Germany. So it is ( according to you) Dr. Stefan
Eicker
who is simplistic and wrong.


No, HE's not wrong and/or simplistic because:
1. He doesn't say (see point 2, 3 in my reply) what YOU originaly posted,
except for one point (see point 1 in my reply) where HE is wrong too.
2. you are refering to "presentations" material which is not intended as
reading material, I never said his "presentations" were simplistic, how
could I, I never attended one. No, I said YOUR description is a bit
simplistic (see point 2, 3).

Next time when refering to anothers material, please add a quote in your
posting, Dr. Stefan Eicker deserves it, and you won't need to refer to it
after the facts.

Willy.

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:#9*************@TK2MSFTNGP10.phx.gbl...

"Gino Cerone" <co****@covad.net> wrote in message
news:u%***************@TK2MSFTNGP09.phx.gbl...
> When an application is opened Windows allocates the full 32 bit address
> space (4 gigs) of memory. (this is theoretical and Windows memory manager > maps from the actual memory to the 32 bit memory space) It sub-divides
> this
> into two parts, the Stack and Heap. The stack holds the declared named
> varibles. In the case of a primative data type like 'byte' it holds the
> value,
>
> byte mybyte;
> mybyte = 125;
>
> In the case of instantiating a class, the declared name varible is writen > to
> the stack. The instaniated Object in writen to the Heap. The value of the > varible in the stack holds the address space of the Object in the Heap.
>
> sqlconnection myconnection; (declares the varible myconnection of type > sqlconnection in the stack. If you tried to use myconnection
> now you would get a null
> reference error because there isn't a memory address in the value yet.)
>
> myconnection = new sqlconnection; (this line instantiates the class
> 'sqlconnection' into an Object in the Heap, and enters the address
> of the Object
> in
> the value for myconnection. myconnection becomes the pointer that
> references
> the Object
> 'sqlconnection' in the Heap.)
>
>
> "shine" <sh***@discussions.microsoft.com> wrote in message
> news:5D**********************************@microsof t.com...
>> what is the difference between a heap and a stack?
>
>


Sorry, but you present a very simplistic view of the real process memory
space and you got it wrong on several points.
1. The full 32 bit address space is not allocated as user process space,
only a maximum of 2GB or 3GB (using /3GB RAM tuning switch in boot.ini)

can
ever be reserved (and possibly committed).
2. This space is not sub-divided into a stack and a heap, there is stack
space allocated per thread created and there is heap space. When the OS
creates a process it always starts with a single thread so your process
always start with a single stack (default size: 4KB committed, 1MB

reserved
space).
The default sizes of heap and stack space is determined by:
- the built process - managed code generators generate defaults (see

later),
using unmanaged code build tools (like link.exe) one can specify other
values for both heap and stack.
- the values assigned by tools like editbin....
The default sizes for ".NET" processes is:
for the heap 4KB committed and 1MB reserved, extra space can (and

will)
be requested by the process at run time.
for the stack 4KB committed and 1MB reserved, the stack can never
grow
beyond the reserved maximum.

3. The stack only holds local variables, not all "the declared named
variables".
4. Primitive types (as all value types) are only stack allocated when
declared local (they have function scope), instance variables are GC heap
allocated. Note that locals can also be register allocated!

5. This:
> sqlconnection myconnection; (declares the varible myconnection of type > sqlconnection in the stack. If you tried to use myconnection
> now you would get a null > reference error because there isn't a memory address in the value yet.)

is not completely true, the compiler will not allow you to use an
uninitialized local variable. If he would allow it, the variable could

hold
any possible value that was left on the stack at the location of the
variable.

Willy.


Nov 17 '05 #10

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

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
1
by: Geiregat Jonas | last post by:
I'm reading Eric Gunnerson's book. He is talking about the heap and stack, he says you have 2types, value wich are in the stack or inline or reference types wich are in the heap. I don't get...
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
3
by: nahur | last post by:
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
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
9
by: Roman Mashak | last post by:
Hello, I'm confused about heap and stack memories management in C language. Most books explain that local stack variables for each function are automatically allocated when function starts and...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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.