Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 about Linux?

EDIT:

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

like image 267
Lukasz Madon Avatar asked Apr 20 '11 09:04

Lukasz Madon


People also ask

What kind of address is returned by malloc?

The function MALLOC() allocates an area of memory and returns the address of the start of that area. The argument to the function is an integer specifying the amount of memory to be allocated, in bytes. If successful, it returns a pointer to the first item of the region; otherwise, it returns an integer 0.

What information can malloc return?

Return Value malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void , use a type cast on the return value.

Does malloc use memory?

In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.

Where does malloc get space?

Normally, malloc() allocates memory from the heap, and adjusts the size of the heap as required, using sbrk(2).


2 Answers

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.

like image 150
Hans Lub Avatar answered Oct 10 '22 04:10

Hans Lub


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.
like image 20
R.. GitHub STOP HELPING ICE Avatar answered Oct 10 '22 04:10

R.. GitHub STOP HELPING ICE