On Linux if I were to malloc(1024 * 1024 * 1024), what does malloc actually do?
I\'m sure it assigns a virtual address to the allocation (by walking the
Malloc allocates memory out of blocks managed by libc. When additional memory is needed the library goes to the kernel using the brk system call.
The kernel allocates pages of virtual memory to the calling process. The pages are managed as part of resources owned by the process. Physical pages are not allocated when memory is brk'd. When the process accesses any memory location in one of the brk'd pages a page fault occurs. The kernel validates that the virtual memory has been allocated and proceeds to map a physical page to the virtual page.
Page allocation is not limited to writes and is quite distinct from copy on write. Any access, read or write, results in a page fault and mapping of a physical page.
Note that stack memory is automatically mapped. That is, an explicit brk is not required to map pages to virtual memory that is used by the stack.