views:

199

answers:

5

Just like in the title, what is a contiguous memory block?

+14  A: 

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.

Steve Jessop
+1 Nice figure.
Sinan Ünür
@Sinan: thanks, I work out.
Steve Jessop
+1 for that response, classic.
Platinum Azure
+3  A: 

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.

CodeInChaos
+2  A: 

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.

Sinan Ünür
+1  A: 

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.

Peter G.
+3  A: 

This is a contiguous memory block of five bytes, spanning from location 1 to location 5:

alt text

It represents bytes (in 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:

alt text

It is fragmented into three groups of bytes (yellow), starting at location 1 with gap bytes between the groups at locations 4 and 6.

Michael Goldshteyn