开发者

malloc returns memory or virtual address space

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 ab开发者_JAVA技巧out Linux?

EDIT:

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


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.


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.


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.


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.


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.


  • 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.


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 .


All processes run within its own virtual address space. Each access to memory is mediated by the memory management unit. If memory is mapped, the data is either loaded or stored from the corresponding physical address. If no memory is mapped to the specified address, the (Memory Management Unit (MMU) will trigger an exception.

Malloc manages a bunch (or perhaps even just a fraction) of mapped memory pages. These pages are known as the heap. When one requests a number of bytes from malloc, malloc will either find that memory within the pages that it already manages or it will ask the operating system (using either brk or mmap on linux). This is totally transparent to the user of malloc.

So the two concepts are totally orthogonal. Processes access virtual memory which the MMU may translate into a physical address and the heap is the block of memory managed by malloc.


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.


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.


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.


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.


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?


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.)


Kernel and user space work with virtual addresses (also called linear addresses) that are mapped to physical addresses by the memory management hardware. This mapping is defined by page tables, set up by the operating system.

Go through this link on Memory Allocation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜