malloc returns memory or virtual address space

15,964

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
15,964
Lukasz Madon
Author by

Lukasz Madon

I like python. Software Engineer @ rolepoint.com SOreadytohelp

Updated on June 16, 2022

Comments

  • Lukasz Madon
    Lukasz Madon almost 2 years

    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 legacy of DOS? How about Linux?

    EDIT:

    many answers with great details, none of them answer my question, though.

    • Erik
      Erik about 13 years
      Virtual address space is a way of mapping memory as seen by something to memory as seen by something else. You don't allocate virtual address space, you allocate memory, and memory is mapped into your address space.
    • pmg
      pmg about 13 years
      The Standard refers to "storage": no heap, no virtual address space, no tables, ...
    • Lukasz Madon
      Lukasz Madon about 13 years
      So the correct statment is "Thread consumes 1MB of memory" and inncorrect is "Thread consumes 1MB virtual address space"?
    • R.. GitHub STOP HELPING ICE
      R.. GitHub STOP HELPING ICE about 13 years
      See the update to my answer. The best description is that it consumes 1MB of anonymous virtual memory.
  • KeyC0de
    KeyC0de almost 7 years
    Under the cover of an OS, a process has no way of knowing the physical memory. It only knows about virtual memory. Only the MMU knows physical memory and the software programmer is never concerned with this. malloc allocates virtual memory yes. Commit charge is an OS mechanism yes. I don't understand why you have involved physical memory here. If you meant to say that actual physical memory is consumed, well of course it does, because virtual memory is mapped to physical memory.
  • loin.liao
    loin.liao almost 5 years
    very clear!the return address of malloc can be a real address or virtual address, depends on OS. And it is meaningless to caller(application), it is only meaning for OS. and its only scene to be used is as a arguments of read/write operation to OS. so only OS use it, interprent it. so its value is meaningless to Application. Because OS generate that address, and ONLY OS use it.