malloc returns memory or virtual address space

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

    malloc() specifically allocates from the heap.

    Whether the heap memory is in a virtual address space or not depends entirely on the operating system and hardware architecture. On a system with an MMU and an operating system that utilises it, all memory (heap, code space, stacks, static memory and memory mapped I/O etc.), exists in a virtual space, even if the physical to virtual mapping is one-to-one.

    To have a virtual address space requires an MMU to map physical to virtual addresses, not all targets have an MMU, so heap memory and virtual memory are not synonymous or interchangeable concepts in any way; they are entirely independent concepts.

    With respect to "Virtual Address Space" being a "the legacy of DOS", you could not be further from the truth, 16 bit x86 architecture does not support an MMU or virtual memory at all. I wonder how you got that idea?

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

    malloc allocates a block on the heap. For each memory page that the allocated block spans, there may or may not be physical memory committed to it at the outset. The entire block is usable though, since the OS will take care of handling page faults and managing physical/virtual memory that's needed to support the allocation.

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

    The answer depends on the underlying OS, libc implementation, and hardware architecture. With most modern OS'es (like Linux or Windows) running on the x86 architecture you will get a pointer within the linear address space, but generally this is implementation dependent. I doubt that when, for example, programming some small device (like a microcontroller) in C, malloc() would return a pointer to virtual memory, because there is no virtual memory as such.

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