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): class ExampleOne { struct example { public int numerator; public int denominator; } static void Main() { example ex1 = new example(); object o = ex1; example ex2 = (example)o; ex2.denominator++; ex2.numerator++; object o2 = ex2; Console.WriteLine(ex2.denominator); Console.WriteLine(ex2.numerator); } }
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.(?)
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.
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.