malloc returns memory or virtual address space

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

    malloc() allocates the virtual memory, owned by the process.

    During execution the process can be reloaded to the physical memory several times by the operating system. The operating system maps virtual addresses of each process to the physical memory. A process doesn't know the mapping.

    Virtual address space space consist of

    • system space (drivers) and
    • user space (a program code area, a heap for dynamic memory allocation, and a stack for variables, arguments, return values, etc.)
    0 讨论(0)
  • 2021-01-31 07:14

    There are at least 3 ways of measuring memory consumption:

    • virtual address space - the amount of your process's address space consumed by the allocation. this also affects fragmentation and the maximum contiguous future allocations you can make.
    • commit charge - this is the operating system's accounting of the maximum possible physical storage required to maintain all of the writable, non-file/device-backed memory allocated to your process. if the OS allows it to exceed the total physical memory + swap, very bad things could happen the first time the excess is written to.
    • physical memory - the amount of physical resources (potentially including swap, depending on your interpretation) your process is currently occupying. This could be less than commit charge due to virgin zero pages and virgin private writable maps of files, or more than commit charge due to non-writable or shared mappings the process is using (but these are usually swappable/discardable).

    malloc generally affects them all.

    Edit: So, the best way I can think to answer your question is to say:

    malloc allocates virtual memory.

    And virtual memory consumes:

    • virtual address space,
    • commit charge, and
    • physical resources, if it's been written to.
    0 讨论(0)
  • 2021-01-31 07:15

    Malloc allocates on the heap, which is a part of the virtual address space.

    You are not being picky by calling it virtual address space, you are just being too general. It can be compared to saying, "You puke in the bathroom." Strictly speaking that is true but "You puke in the pot" is more precise because the former statement implies that you can puke in the sink or tub too.

    Conceptually, malloc, the heap and virtual memory are supported by most operating systems including Dos and Linux.

    0 讨论(0)
  • 2021-01-31 07:16
    • malloc() does allocate a block of memory on the HEAP.

    • Should it be called virtual address space? Hold that thought for a second. VAS (virtual address space) is a memory mapping mechanism that comprises the entire memory space of an application. In other words, VAS is not restricted to the memory area of the HEAP. The HEAP is actually just another part of it.

    Each time a new application is run, the OS creates a new process and allocates a new VAS for the application. Memory allocated through malloc() is reserved on the HEAP, which is a special memory region within the VAS, as you know, and memory allocated through standard means ends up in the stack, which is another region of memory located inside the VAS of the application.

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

    malloc allocates memory on the heap, period.

    Your C library typically keeps a list (or some more intricate data structure) of available memory chunks, finding a suitable chunk to satisfy a malloc (possibly splitting a larger chunk into a number of smaller ones) and returning free'd memory to the list (possibly merging a few smaller chunks into a bigger one)

    Only when the list doesn't contain a large enough chunk to satisfy your malloc, the library will ask the OS for more memory, e.g. using the sbrk syscall. The address returned by this syscall may be a virtual address, or a real one, depending on your hardware, but as a programmer you cannot (and don't need to) know this.

    Saying that malloc allocates virtual adress space rather than a block on the heap is like saying that read reads from your hard disk rather than from a file: it is irrelevant from the caller's perspective, and not always true.

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

    Malloc always returns virtual address, the reason is that when you call malloc it's actually a wrapper function which calls a system call (system call is a fancy word for kernel level instructions) and this system call allocates a virtual memory inside of your heap segment. But when you want to access the allocated value (store or load instruction) MMU will raise Page fault which basically means there is no physical memory for this virtual page, and only at that time OS will allocate physical memory for this virtual page .

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