rockstar - call stack vs heap Flashcards
1
Q
Call stack
A
- Data structure for managing method calls
- Keeps track of sequence of function calls
- When a method call is added to stack, it’s variable(s) memory gets allocated to the stack as well
- When the method call is over, the variable(s) memory gets de-allocated
2
Q
Heap - dynamic memory allocation
A
- Region of memory used for dynamic memory allocation
- Objects created on heap persist beyond the scope of methods or functions that created them
- They can be accessed by multiple parts of your program
3
Q
Heap - reference types
A
- Reference types (such as objects, arrays, etc.) are typically allocated on the heap
- The memory for these objects are managed by the .NET runtime’s garbage collector -> which determines when objects are no longer needed -> then reclaims their memory
4
Q
Summary
A
- Call stack is mechanism for managing execution flow and local variables during method calls
- Heap is region of memory used for dynamic memory allocation where objects with longer lifetimes are stored
5
Q
How arrays are accessed in memory
A
- Reference type arrays (such as objects, strings, or instances of classes) are allocated on the heap
- Value type arrays (such as integers, doubles, or structs):
Allocated on stack if the variable is local
Allocated on heap if the variable is created within a class
6
Q
How arrays are stored in memory
A
- Adjacent blocks of memory stored sequentially
- Depending on the data type, allocates a block of memory of that data types size
- Base address of array is where the first element is stored