malloc returns memory or virtual address space

前端 未结 15 1834
攒了一身酷
攒了一身酷 2021-01-31 06:36

Does malloc allocate a block of memory on the heap or should it be called Virtual Address Space?

Am I being picky calling it Virtual Address Space or this just the legac

15条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 07:01

    All processes run within its own virtual address space. Each access to memory is mediated by the memory management unit. If memory is mapped, the data is either loaded or stored from the corresponding physical address. If no memory is mapped to the specified address, the (Memory Management Unit (MMU) will trigger an exception.

    Malloc manages a bunch (or perhaps even just a fraction) of mapped memory pages. These pages are known as the heap. When one requests a number of bytes from malloc, malloc will either find that memory within the pages that it already manages or it will ask the operating system (using either brk or mmap on linux). This is totally transparent to the user of malloc.

    So the two concepts are totally orthogonal. Processes access virtual memory which the MMU may translate into a physical address and the heap is the block of memory managed by malloc.

提交回复
热议问题