malloc returns memory or virtual address space

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

提交回复
热议问题