malloc returns memory or virtual address space

前端 未结 15 1813
攒了一身酷
攒了一身酷 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.

    0 讨论(0)
  • 2021-01-31 07:03

    To answer this question, we need to know what kind of Operating System and architecture we are dealing with. As pmg mention the Standard and articles refers to “storage” or “space”. These are the most general terms and the only valid ones, unless we make a bunch of assumptions.

    For example:

    malloc allocates a block of Virtual Address Space on the heap

    This is not correct for many embedded systems. Many of them do not use Virtual Memory, because there is no need (no multitasking etc.) or for performance reasons. What is more, it is possible that some exotic device does not have the idea of heap – doubt that malloc would be used, but fairly that is one of the reasons why the Standard refers to “storage” - it is implementation-specific.

    On the other hand, the example is correct for Window and Linux in our PCs. Let's analyze it to answer the question.

    First off, we need to define what is Virtual Address Space.

    Virtual Address Space (VAS) is a memory mapping mechanism that helps with managing multiple processes.

    • isolate processes – each of them has his own address space
    • allow to allocate memory that 32-bit architecture is limited to.
    • provides one concise model of memory

    Back to question, ”Does malloc allocate a block of memory on the heap or should it be called Virtual Adress Space ?”

    Both statements are correct. I would rather say VAP instead of memory – it is more explicit. There is a common myth that malloc = RAM memory. Back in old day, DOS memory allocation was very simple. Whenever we ask for memory it was always RAM, but in modern Oses it may vary.

    0 讨论(0)
  • 2021-01-31 07:09

    Kernel and user space work with virtual addresses (also called linear addresses) that are mapped to physical addresses by the memory management hardware. This mapping is defined by page tables, set up by the operating system.

    Go through this link on Memory Allocation.

    0 讨论(0)
  • 2021-01-31 07:10

    Does malloc allocate a block of memory on the heap or should it be called virtual adress space?

    short answer: malloc allocates memory on the heap.

    it's not precise enough to say that malloc allocates memory in the virtual adress[sic] space, since your call stack itself is part of that same space.

    0 讨论(0)
  • 2021-01-31 07:10

    You could have answered this question yourself if you had bothered to RTFM :-)

    In particular, typing man malloc on a Linux machine and searching (one-at-a-time) for "heap" and "virtual" will let you see unambiguously that malloc() is defined in terms of heap memory, rather than virtual memory.

    The Wikipedia article for malloc() agrees with the Linux man page. It states (emphasis is mine):

    In C, the library function malloc is used to allocate a block of memory on the heap. [...] Some platforms provide library calls which allow run-time dynamic allocation from the C stack rather than the heap (e.g. Unix alloca(), Microsoft Windows CRTL's malloca()). This memory is automatically freed when the calling function ends. The need for this is lessened by changes in the C99 standard, which added support for variable-length arrays of block scope having sizes determined at runtime.

    If you are confused about the meaning of terminology, then the Wikipedia articles on heap memory and virtual memory may help you.

    0 讨论(0)
  • 2021-01-31 07:12

    malloc is a library call. On linux, it in turn calls sbrk system call. sbrk will increase the size of heap but does not actually allocate physical memory. When the process tries to access this address, a page fault is raised and then at that time kernel allocates actual physical page and maps to the virtual address.

    TL;DR: malloc returns a virtual address and does NOT allocate physical memory.

    Check this out.

    0 讨论(0)
提交回复
热议问题