B2.2.1
Compare static and dynamic data structures
Have a fixed size determined at compile time — size cannot change during execution.
Static Data Structures
Have a fixed size determined at compile time — size cannot change during execution.
| Speed | Faster access due to contiguous memory allocation |
| Memory | Predictable usage; fixed size |
| Overhead | No runtime allocation cost |
Disadvantages:
- Inflexible size — cannot grow or shrink
- Potential memory waste if overallocated
- Cannot adapt to changing data needs
Dynamic Data Structures
Can grow or shrink during execution — memory is allocated at runtime.
| Size | Flexible; adapts to runtime requirements |
| Memory | Allocates only what’s needed |
| Ease | Easier to work with in practice |
Disadvantages:
- Slightly slower due to resizing
- Runtime allocation overhead
- More complex implementation
Comparison Table
| Feature | Static | Dynamic |
|---|---|---|
| Size | Fixed at compile time | Changes at runtime |
| Speed | Faster access | Slightly slower |
| Memory | Predictable, may waste space | Efficient, may fragment |
| Flexibility | Inflexible | Adaptable |
| Implementation | Simpler | More complex |
| Example (Python) | tuple | list, dict, set |