Course contentsShow
← Computer Science
Lesson 238 of 2,8726. Arrays and Basic CollectionsFree lesson

Static vs Dynamic Arrays

Comparing fixed-size arrays with dynamic arrays that can grow or shrink at runtime.

Static vs Dynamic Arrays

What you'll learn: You'll understand the difference between arrays with a fixed size and arrays that can grow or shrink as your program runs.

The Core Difference

When you declare an array, you face a fundamental choice: static arrays have a size that's locked in at creation time and never changes, while dynamic arrays can expand or contract during program execution.

Think of a static array like a parking lot with painted lines—once those 10 spaces are marked, you can't add an 11th without rebuilding. A dynamic array is more like a flexible event venue that can add or remove chairs as guests arrive.

Static Arrays

Static arrays require you to specify their size upfront, either when you write the code or when the program runs (but before you use the array). That size becomes permanent. If you declared an array with 5 elements, it will always hold exactly 5 elements—no more, no less.

The advantage? They're simple and fast. The memory is allocated once, and the computer knows exactly where everything lives.

The drawback? You must know (or guess) how much space you'll need in advance. Too small and you can't fit all your data; too large and you waste memory.

Dynamic Arrays

Dynamic arrays solve the sizing problem by allowing the array to grow when you add elements beyond its current capacity. Behind the scenes, the program typically allocates a new, larger block of memory, copies existing elements over, and releases the old memory.

The advantage? Flexibility. You can start small and expand as needed.

The drawback? Growing the array takes time and extra memory during the copy operation.

Key Takeaway: Static arrays have fixed sizes determined at creation and are simple but inflexible, while dynamic arrays can resize at runtime, trading some performance overhead for adaptability.