C语言 什么是连续内存块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4059363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:51:44  来源:igfitidea点击:

What is a contiguous memory block?

cmemoryheapheap-fragmentation

提问by 565471741

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

就像标题一样,什么是连续内存块?

回答by Michael Goldshteyn

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

这是一个连续的五个字节的内存块,从位置 1 到位置 5:

alt text

替代文字

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:

这是一组非连续的五个感兴趣的字节:

alt text

替代文字

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.

它被分成三组字节(黄色),位置 4 和 6 有间隙字节。从位置 1 开始,有一个连续的三个字节块,从位置 1 到 3。还有两个块,每个块一个字节分别位于位置 5 和 7。

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.

位置 0 处未使用的块以及位置 7 之外的任何后续块通常可以被忽略,因为它们确实介于从位置 1 到 7 的感兴趣字节之间。

回答by Steve Jessop

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):

该术语出现在数组的定义中是“连续的”。这意味着元素是端到端布局的,没有不连续性,它们之间没有填充(每个元素内部可能有填充,但元素之间没有填充)。所以一个由 5 个 4 字节元素组成的数组看起来像这样(每字节 1 个下划线字符,| 符号不代表内存):

 ____ ____ ____ ____ ____
|____|____|____|____|____|

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.

在所有情况下,“看起来像”意味着“就 C 中可见的地址而言”。某些东西在虚拟地址空间中可能是连续的,但在物理 RAM 中却不是连续的。就此而言,某些东西在物理 RAM 地址空间中可能是连续的,但在物理 RAM 中实际上并不相邻。其中一半可能在这边的一个 RAM 芯片上,另一半在那边的另一个 RAM 芯片上。但是 C 内存模型不能“看到”任何这些。

回答by CodesInChaos

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.

不被其他内存中断的内存块。或者更准确地说,它需要一个不间断的虚拟地址空间块。支持该地址空间的真正 RAM 不需要是连续的。

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.

这在 64 位应用程序上不是什么大问题,因为那里的地址空间很大。但是在 32 位进程中,可能会发生堆变得如此碎片化(在已释放的块之间仍有未释放的块),从而导致更大的分配失败。

回答by Peter G.

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.

当一个内存块由来自单个线性地址空间的开始和结束地址定义并且没有空洞时,它就是连续的。

回答by Sinan ünür

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.

在分配内存的上下文中回答,当您调用内存分配器并请求 24 字节的内存时,它必须能够定位至少 24 字节的未分配内存的单个块。

If it has 16 bytes of memory starting at one address xand another 8 bytes starting at an address ysuch that y > x + 16or 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.

如果它有 16 个字节的内存从一个地址开始,x另外 8 个字节从一个地址开始,y这样y > x + 16y < x - 8(这会产生一个间隙),那么分配器不能满足您对 24 个字节的请求,即使总共有 24 个可用字节.

See also Fragmentation.

另见分

回答by Ramzan

Memory consists of a large number of blocks of bytes. Each block has its own address. When a process requests for memory, memory is allocated in two ways:

内存由大量字节块组成。每个块都有自己的地址。当进程请求内存时,内存分配有两种方式:

  1. Contiguous Memory Allocation
  2. Non-Contiguous Memory Allocation
  1. 连续内存分配
  2. 非连续内存分配

In contiguous memory allocation, consecutive (one after Another) blocks are assigned which are called contiguous Memory blocks.
In non-contiguous memory allocation, separate blocks present at different locations are assigned.

在连续内存分配中,连续(一个接一个)块被分配,称为连续内存块。
在非连续内存分配中,分配存在于不同位置的单独块。