cmemoryheap-memoryheap-fragmentation

What is a contiguous memory block?


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


Solution

  • 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.