Monday, 27 April 2015

The Heap and The Stack

The Heap and The Stack

Procedural programming languages that let you to create variables dynamically at run-time use two different areas of Ram for holding variables;. the Stack and the Heap. The Heap is basically all unallocated memory.

The Stack holds value type variables plus return addresses for functions. All numeric types, ints, floats and doubles along with enums, chars, bools and structs are value types.

The Heap hold variables created dynamically- known as reference variables and mainly instances of classes or strings. These variables are stored in two places; there's a hidden pointer to the place in the heap where the data is stored.

Another distinction between value and reference type is that a value type is derived from System.ValueType while a reference type is derived from System.Object.

If you assign a value type variable to another then a direct copy of the value is made. But copying a reference type variable just makes a copy of the reference to the variable and does not affect the variable itself. This is like pointers in C and C++. You aren't copying what the pointer points to but making a copy of the pointer itself.

No comments:

Post a Comment