┌─────────────────────────────────────┐ ← High Address
│ STACK │
│ ↓ (grows downward) │
│ • Local variables │
│ • Function parameters │
│ • Pointers to heap data │
├─────────────────────────────────────┤
│ FREE SPACE │
├─────────────────────────────────────┤
│ HEAP │
│ ↑ (grows upward) │
│ • Dynamic allocations (new) │
│ • Large data structures │
└─────────────────────────────────────┘ ← Low Address

int main() {
int a = 10; // Stack
char arr[100]; // Stack
double d = 3.14; // Stack
return 0; // All automatically destroyed
}
int n;
cin >> n;
int arr[n]; // BAD - Variable Length Array (VLA)
//VLAs are not part of standard C++, only a compiler extension (e.g., GCC).
Problems:
Better alternatives:
// Option 1: Heap allocation
int* arr = new int[n];
delete[] arr;
// Option 2: Vector (recommended)
vector<int> arr(n);