C++20 sections · 1024 units
Open in Course

Problem - Flexible Array

Heap memory basics

You'll read nn from input, then allocate an array of exactly nn elements. This teaches the new operator for dynamic allocation and the importance of memory cleanup with delete.

Static arrays require sizes known at compile time. Dynamic arrays live on the heap and can have any size. You request memory with new and return it with delete. Forgetting delete causes memory leaks; using memory after delete causes crashes.

This problem demonstrates allocating, using, and freeing a dynamic array. The bracket in delete[] is required for arrays to free all elements.