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

C#

 
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 
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 
Ut oh... this may require ASCII art, always dangerous. Here goes:
class Example {
  int c;
  int d = 3;
  static int f;
  static void Main() {
    Example e = new Example();
    e.Foo(0);
  }
  void Foo(int a) {
    int b = Bar(a);
    c = b;
    b += Bar(a, this);
  }
  int Bar(int a) {
    string s = "Test";
    return a + 42;
  }
}
Given the code above, here's what the stack would look like
just as Bar as been called for the second time.
I've oversimplified a _lot_ of things, but this covers the important points.

             Stack                               Heap
Main's +------------------+
Stack- |address of cleanup|
 frame |Main.e variable   =--+    
       +------------------+  |    Example Object       Example Class
 Foo's |address of Main   |  |  +------------------+    +---------------+
Stack- |'this' pointer    =--+->|address of class  |=-->|address of Foo |
 frame |Foo.a parameter  0|  |  |Example.c field 42|    |address of Bar |
       |Foo.b variable  42|  |  |Example.d field  3|    |Example.f field|
       +------------------+  |  +------------------+    +---------------+                
 Bar's |address of Foo    |  |       String Object     String Object
Stack- |'this' pointer    =--+      +-------------+   +---------------+
 frame |Bar.a parameter 42|     +-->|various      |   |string from 1st|
       |Bar.s variable    =-----+   |string fields|   |call to Bar    |
       +------------------+         +-------------+   +---------------+
Sequence of Events:
1) When the code started there was just Main's stackframe.
2) When Foo was called, Foo's stackframe was added below Main's.
3) When Bar was called the first time, a stackframe for it was created below Foo's.
4) When Bar returned the first time this stackframe was removed.
5) When Bar was called a second time, a new stackframe was created. (It happens to be in the same place as the first one, but doesn't have to be.)

Things to Note:
~ Those int values in variables and parameters are stored on the stack.
~ The references to objects of type example are stored on the stack and point into the heap.
~ Instance variables like Example.c and Example.d are stored with the object on the heap.
~ Static variables like Example.f are stored in one place that all instances have a pointer to.
~ The string object created during the first call to Bar is no longer referenced and the garbage collector will clean it up as needed.


-Blake
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 
GeneralRe: int value of all WM_... messages Pin
J. Dunlap25-Oct-03 11:39
J. Dunlap25-Oct-03 11:39 
GeneralRe: int value of all WM_... messages Pin
Nick Parker25-Oct-03 11:56
protectorNick Parker25-Oct-03 11:56 

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.