malloc returns memory or virtual address space

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

提交回复
热议问题