What is a contiguous memory block?

眉间皱痕 提交于 2019-11-27 03:44:28

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.

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.

A block of memory which isn't interrupted by other memory. Or to be more precise it requires an uninterrupted block of virtual address-space. The real RAM backing that address space doesn't need to be contiguous.

This is important if you allocate a large memory block. The OS has to give it to you as a contiguous block, but if memory is so fragmented that only smaller pieces are free then this memory allocation cannot be satisfied even if the total free memory is larger than the requested space.

This isn't such a big problem on 64 bit apps since the address space is large there. But in 32 bit processes it can happen that the heap gets so fragmented(between freed blocks there are still unfreed blocks) that larger allocations fail.

Answering in the context of allocating memory, when you invoke the memory allocator and request 24 bytes of memory, it must be able to locate a single block of at least 24 bytes of unallocated memory.

If it has 16 bytes of memory starting at one address x and another 8 bytes starting at an address y such that y > x + 16 or y < x - 8 (that would create a gap), then the allocator cannot satisfy your request for 24 bytes even though there is a total of 24 bytes free.

See also Fragmentation.

A memory block is contiguous exactly when it is defined by a start and an end address from a single linear address space and has no holes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!