0

I recently asked a question about StackOverflowExeptions and the explanations were very helpful!
However, I wrote a method and tried to figure out where T cached is allocated (heap/stack):

private Dictionary<Type, Component> _cachedComponents = new Dictionary<Type, Component>();

public T GetCachedComponent<T>() where T : Component {
   //Not yet sure if the next line works or throws an exception -> just ignore it
    if(_cachedComponents[typeof(T)] != null) {
        return (T)_cachedComponents[typeof(T)]
    } else {
        T cached = this.GetComponent<T>();
        _cachedComponents.Add(typeof(T), cached);
        return cached;
    }
}
  1. Since T cached is declared inside the method I assume it is allocated on the stack, right?
  2. But the reference is then added to the dictionary, wich should be allocated on the heap, right?
  3. The stack is "cleared" after the method returns, right?
  4. But what happens to T cached?
    Is it going to be moved to the heap?
    (Because the stack does not "carry the data" anymore but the dictionary still holds a reference)
trincot
  • 317,000
  • 35
  • 244
  • 286
Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
  • Aaaaaaah. No. You should really look at the various questions about reference types and value types. – xanatos May 27 '15 at 13:49
  • 2
    [The stack is an implementation detail](http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx) trying to think of programs using it isn't usually the right mindset. – Servy May 27 '15 at 13:50
  • possible duplicate of [Memory allocation: Stack vs Heap?](http://stackoverflow.com/questions/4487289/memory-allocation-stack-vs-heap) – Black Frog May 27 '15 at 13:52
  • This guy explains things very well: https://www.youtube.com/watch?v=lLbKkClhzbU&list=PLRwVmtr-pp07XP8UBiUJ0cyORVCmCgkdA&index=4 – Dennis_E May 27 '15 at 13:52

1 Answers1

0

Since T cached is declared inside the method I assume it is allocated on the stack, right?

T being allocated in a method doesn't effect it being on the heap or the stack. The decision whether it is the former or the latter is based on whether this is a reference type or a value type.

But the reference is then added to the dictionary, wich should be allocated on the heap, right?

Once the reference is added to the Dictionary<TKey, TValue>, the key will be stored inside the dictionary, which is allocated on the heap, as it is a reference type.

The stack is "cleared" after the method returns, right?

The stack frame is cleared once the method returns.

But what happens to T cached? Is it going to be moved to the heap?

If T is cached inside the dictionary, then it is already allocated on the heap.

Overall, I assume you ask these questions as for general knowledge. You shouldn't be worrying about this too much, as what I wrote here is an implementation detail and is subject to change.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Yes, this is for general knowledge. :D (You know how it works - I'd like to too) Hmm, But a value type field inside the class body would also be allocated on the heap, wouldn't it? Aside from that I understood your input :) thx a lot – Noel Widmer May 27 '15 at 13:56
  • If it is a *local variable*, it would be allocated on the stack. If it is a *field* inside the class declaration, it would be allocated on the heap. – Yuval Itzchakov May 27 '15 at 13:57