Just like in the title, what is a contiguous memory block?
What Does Contiguous Memory Allocation Mean? Contiguous memory allocation is a classical memory allocation model. Here, a system assigns consecutive memory blocks (that is, memory blocks having consecutive addresses) to a process. Contiguous memory allocation is one of the oldest memory allocation methods.
It means that memory is allocated as a single chunk. This is most often used when talking about containers. For instance, the vector and string classes use a contiguous chunk of memory.
A file on disk that is not broken apart. All sectors are adjacent to each other. A contiguous file is faster to read and write than one that is fragmented across the disk.
Contiguous memory allocation contains two memory allocations: single partition and multi-partition. It contains Paging and Segmentation. The memory space is partitioned into fixed-sized partitions in the contiguous memory allocation, and each partition is only assigned to one process.
This is a contiguous memory block of five bytes, spanning from location 1 to location 5:
It represents bytes (colored light blue) that are together in memory with no gap bytes (white) between them.
This is a non-contiguous set of five bytes of interest:
It is fragmented into three groups of bytes (colored yellow) with gap bytes at locations 4 and 6. Starting at location 1 there is a contiguous block of three bytes, spanning from locations 1 to 3. There are two more blocks of one byte each at locations 5 and 7, respectively.
The unused block at location 0 as well as any subsequent blocks beyond location 7 can usually be ignored since they do interpose between the bytes of interest spanning from locations 1 to 7.
One without any gaps in the addresses it occupies. You can probably just think of this as a "block", and think of something with a gap in the middle as "two blocks".
The term comes up in the definition of an array as being "contiguous". That means the elements are laid out end-to-end, with no discontinuities and no padding between them (there may be padding inside each element, but not between elements). So an array of 5 4-byte elements looks like this (1 underscore character per byte, the | symbols don't represent memory):
____ ____ ____ ____ ____ |____|____|____|____|____|
It doesn't look like this:
____ _ ____ _ ____ _ ____ _ ____ |____|_|____|_|____|_|____|_|____|
And neither does it look like this:
____ ____ ____ ____ ____ |____|____|____| ... somewhere completely different ... |____|____|
In all cases, "looks like" means "as far as the addresses visible in C are concerned". Something could be contiguous in virtual address space, but not contiguous in physical RAM. For that matter, something could be contiguous in physical RAM address space, but not actually adjacent in physical RAM. Half of it could be on one RAM chip over here, and the other half on another RAM chip over there. But the C memory model can't "see" any of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With