Bifalgorithm post · Computer science one · Contiguous 2-D Arrays
Why use a contiguous memory block for a two-dimensional array?
Why use a contiguous memory block for a two-dimensional array? (Computer science one · Contiguous 2-D Arrays) — "The example depicted in Figure 20.1 constructs…
Ideas in this flowchart
- Constructing a two-dimensional array with contiguous memory
- The example initially constructs a two-dimensional array.
- Each row is created by an independent call to malloc().
- The rows may occupy non-contiguous memory blocks.
- Non-contiguous rows are generally not a problem because malloc() is usually efficient.
- Some scenarios may benefit from one contiguous memory block.
- Contiguous storage can be achieved with one malloc() call and pointer manipulation.
- The initial malloc() for the pointer-to-pointers setup remains unchanged.
- Repeated allocations inside a for loop are replaced by one allocation.
- The single allocation requests enough bytes for the entire n × m array.
- The allocated result is stored at the beginning of the array, at index zero.