Click here to Skip to main content
15,891,762 members
Home / Discussions / C#
   

C#

 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 2:10
Blake Coverett26-Oct-03 2:10 
GeneralRe: static constructors Pin
R. Thomas26-Oct-03 2:24
R. Thomas26-Oct-03 2:24 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 2:39
Blake Coverett26-Oct-03 2:39 
GeneralRe: static constructors Pin
R. Thomas26-Oct-03 3:46
R. Thomas26-Oct-03 3:46 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 4:09
Blake Coverett26-Oct-03 4:09 
GeneralRe: static constructors Pin
Heath Stewart26-Oct-03 5:58
protectorHeath Stewart26-Oct-03 5:58 
Generalstack and heap Pin
R. Thomas26-Oct-03 0:49
R. Thomas26-Oct-03 0:49 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 2:27
Blake Coverett26-Oct-03 2:27 
The stack is the linear piece of memory that the operating system and CLR use to maintain the flow of control for a thread. Every thread has its own stack. Each time a method is called the stack grows (traditionally downwards in memory on x86 architectures) as the OS/CLR adds a stackframe.

Each stack frame contains the memory for the functions parameters, it's local variables, and the address to which it will return when complete. (Other more subtle things can be stored on the stack as well, like markers for managed vs unmanaged, assembly evidence, random data that you used stackalloc to reserve space for, etc, but ignore that for now.)

Local variables are created in the stackframe in most languages. This is what enables recursion and scoping of those variables. If you had declared 'struct TinCan' instead of 'class TinCan' the actual value of your object would have been stored on the stack as well, not just a reference to it.

When a method ends, its stackframe is discarded and all data stored there is lost.

The heap is a block of memory managed by the CLR (and allocated out of the OS heap which serves the same purpose for unmanaged processes). From this large block, an application can allocate space for data/objects. The lifetime of an object allocated on the heap is unrelated to the flow of control from method to method. In a managed language like C# the lifetime is controlled by the garbage collection process. In an unmanaged language like C++ the lifetime is controlled by the application which must manually free objects/release data on the heap.

In C# reference types (things declared with 'class') are always allocated on the heap because this permits them to be managed by the garbage collection. Value types (things declared with 'struct') are either allocated on the stack and their lifetime is managed by the flow of execution or they are allocated embedded inside a reference type in which case their lifetime is that of the enclosing object. (A reference to a object is itself a value type and hence follows those rules.)

Hope this helps.

-Blake
GeneralRe: stack and heap Pin
R. Thomas26-Oct-03 2:38
R. Thomas26-Oct-03 2:38 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 3:31
Blake Coverett26-Oct-03 3:31 
GeneralRe: stack and heap Pin
R. Thomas26-Oct-03 3:44
R. Thomas26-Oct-03 3:44 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 3:55
Blake Coverett26-Oct-03 3:55 
Generalremainder Pin
R. Thomas26-Oct-03 0:40
R. Thomas26-Oct-03 0:40 
GeneralRe: remainder Pin
leppie26-Oct-03 0:51
leppie26-Oct-03 0:51 
GeneralRe: remainder Pin
R. Thomas26-Oct-03 0:53
R. Thomas26-Oct-03 0:53 
GeneralRe: remainder Pin
Rakesh Rajan26-Oct-03 1:08
Rakesh Rajan26-Oct-03 1:08 
GeneralRe: remainder Pin
Daniel M. Edwards26-Oct-03 1:11
Daniel M. Edwards26-Oct-03 1:11 
GeneralRe: remainder Pin
R. Thomas26-Oct-03 1:16
R. Thomas26-Oct-03 1:16 
GeneralRe: remainder Pin
Daniel M. Edwards26-Oct-03 1:17
Daniel M. Edwards26-Oct-03 1:17 
GeneralType.GetMembers() Pin
Arun Bhalla25-Oct-03 15:02
Arun Bhalla25-Oct-03 15:02 
GeneralRe: Type.GetMembers() Pin
Heath Stewart26-Oct-03 6:06
protectorHeath Stewart26-Oct-03 6:06 
GeneralRe: Type.GetMembers() Pin
Arun Bhalla26-Oct-03 8:40
Arun Bhalla26-Oct-03 8:40 
GeneralRe: Type.GetMembers() Pin
Arun Bhalla27-Oct-03 12:01
Arun Bhalla27-Oct-03 12:01 
Generalint value of all WM_... messages Pin
oOomen25-Oct-03 10:25
oOomen25-Oct-03 10:25 
GeneralRe: int value of all WM_... messages Pin
Nick Parker25-Oct-03 11:23
protectorNick Parker25-Oct-03 11:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.