Search Unity

Stack and heap visualizer

Discussion in 'Scripting' started by Wasiim, Jul 29, 2015.

  1. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    So i was bored not much to do when you're 14 (need a life)... so i made a stack and heap visualizer in ms paint(next useless thing after minesweeper...). I'm sure you know but please note this is not how the inside of the RAM looks like. Also it's a boxing and unboxing visualizer as well, not that you care.

    EDIT: I just realized i need to do a boxing and unboxing switcheroo please forgive my ignorance.

    Code (CSharp):
    1.  class ExampleOne
    2.     {
    3.         struct example
    4.         {
    5.            public int numerator;
    6.            public int denominator;
    7.         }
    8.    
    9.         static void Main()
    10.         {
    11.             example ex1 = new example();
    12.             object o = ex1;
    13.             example ex2 = (example)o;
    14.             ex2.denominator++;
    15.             ex2.numerator++;
    16.             object o2 = ex2;
    17.             Console.WriteLine(ex2.denominator);
    18.             Console.WriteLine(ex2.numerator);
    19.  
    20.         }
    21.     }
    Boxing and unboxing.png
     
  2. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    ouch i switched up boxing and unboxing i have such bad luck
     
  3. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Wouldn't think 14-year-olds would concern themselves with stack vs heap allocations. Congrats to you for spending the time to understand it. It's a small step to becoming a black belt programmer.

    EDIT:
    As far as whether or not your visualization is correct, I can't say. Lol. I would've thought there was only one heap allocation with your new call, and everything else was on the stack.(?)
     
  4. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    as far as i'm concerned the value types are on the stack. reference variables such as o are on the stack referencing a "copy" if you will of value types out on the heap.
     
    Last edited: Jul 29, 2015
  5. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    So, this was bothering me in the back of my mind a little bit, and your graphic may not indicate exactly what's happening. If this was expressive of some sort of sequence diagram, it might, but I don't think that's what it's showing. There is only one object allocated on the heap. All other objects are only references to that allocated object. The boxing and unboxing would happen on the same heap allocation, not two different allocations.