The heap holds contiguous chunks of memory allocated while a program runs. It is useful when the required size is unknown until runtime, such as when loading a file whose size may vary widely. The stack, by contrast, holds function-local variables, arguments, and return addresses in stack frames. section 10.3.4
In C++, `new int[count]` allocates an array on the heap using a runtime count, while `int arr[100]` creates a fixed-size array on the stack. A function with the right pointer can access heap memory. In .NET, the garbage collector manages application memory, collecting unused heap chunks; compaction follows collection. section 10.3.4
Listing 10.9 C# code to read numbers into a dynamic array
int[] ReadNInts(int count)
{
int[] arr = new int[count]; #1
for (int i = 0; i < count; i++)
{
arr[i] = readInt(); #2
}
return arr; #3
}
`ReadNInts` allocates an array using `count`, fills it, and returns it—a focused C# example of runtime-sized allocation. section 10.3.4






