1

the memory allocated by malloc could occupy several pages and these pages are not necessarily next to each other, is this correct?

PeopleMoutainPeopleSea
  • 1,492
  • 1
  • 15
  • 24
  • See http://stackoverflow.com/questions/625270/does-malloc-allocate-a-contiguous-block-of-memory. – David Alber Oct 19 '11 at 15:22
  • According to [http://stackoverflow.com/questions/625270/does-malloc-allocate-a-contiguous-block-of-memory][1], yes it is always contiguous. [1]: http://stackoverflow.com/questions/625270/does-malloc-allocate-a-contiguous-block-of-memory – Dirk Dastardly Oct 19 '11 at 15:22
  • 1
    @Drew: the word "physically" is conspicuously absent from that question, and affects the answer. – Steve Jessop Oct 19 '11 at 15:25

3 Answers3

3

That is correct.

Physically contiguous memory only matters for DMA and you don't care because you are not in kernel space.

It will be contiguous in virtual address space.

Joshua
  • 40,822
  • 8
  • 72
  • 132
3

Yes, physically they are not guaranteed to be adjacent, but at least in the process' virtual address space they will appear to be.

In userspace you rarely have to think about physical memory and contiguous pages.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
1

malloc will give you memory thats continous on the virtual address space. If you know how virtual memory works, you know that there are no guarantees that virtual pages are physically continous.

K-ballo
  • 80,396
  • 20
  • 159
  • 169