c# memory managment help pleaseeee

Associate
Joined
12 Mar 2005
Posts
2,021
Location
Scotland
Hi,

For uni we have go some c# memory mangment cw to do. though i have got stuck at a point. ill try to explane:

Some of the code we have to work with:
Code:
   // computes the board positions achieved by 1 more move
   public Board[] NextPositions () {
      Counter nextCounter;
      Board[] nextPositions;
      int     nextBoardPos = 0;

      if ((NumberOfCounters % 2) == 0)
           nextCounter = Counter.X;
      else nextCounter = Counter.O;

      nextPositions = new Board[9-NumberOfCounters];

      for (int row=0; row<3; row++)
         for (int col=0; col<3; col++)
            if (Position[row,col] == Counter.None) {
               nextPositions[nextBoardPos] = new Board (this, row, col, nextCounter);
               ++nextBoardPos;
               int x=0;
               x++;
               Console.WriteLine (x);
            }

      return nextPositions;
   } // end NextPositions method

Now we ahve to discribe the memory mangment of this section in terms of stack and heap.

Dose nextPositions go on the stack when its declaired at the start or dose it only go on the stack + heap when (nextPositions = new Board...)


Any help would be great!


Thanks
 
btt i guess this would apply to any OOP, so even if u dont know c' i think it has the same mm structure.
 
would you like us to just write your homework for you or do you have a specific question?

The stack and heap behavior in the .net runtime are well documented at msdn.Microsoft.com

Paul
 
happytechie said:
would you like us to just write your homework for you or do you have a specific question?

The stack and heap behavior in the .net runtime are well documented at msdn.Microsoft.com

Paul

TheCrow said:
Dose nextPositions go on the stack when its declaired at the start or dose it only go on the stack + heap when (nextPositions = new Board...)

I dont really think that is asking you to do my work for me?? I also thought it was quite specific. No?
 
When you allocate using new, you are allocating on the heap. References to objects however are stored on the stack.

Im not sure about c# but in java when your objects go out of scope when they get cleaned up by the garbage collector depends on the implementation of the virtual machine.
 
Back
Top Bottom