Stack and Heap locations in RAM

Your picture is not of the RAM, but of the address space of some process in virtual memory. The kernel configures the MMU to manage virtual memory, provide the virtual address space of a process, and do some paging, and manage the page cache.

BTW, it is not the compiler which grows the stack (so your picture is wrong). The compiler is generating machine code which may push or pop things on the call stack. For malloc allocated heap, the C standard library implementation contains the malloc function, above operating system primitives or system calls allocating pages of virtual memory (e.g. mmap(2) on Linux).

On Linux, a process can ask its address space to be changed with mmap(2) -and munmap and mprotect. When a program is started with execve(2) the kernel is setting its initial address space. See also /proc/ (see proc(5) and try cat /proc/$$/maps....). BTW mmap is often used to implement malloc(3) and dlopen(3) -runtime loading of plugins, both heavily used in the RefPerSys project (it is a free software artificial intelligence project for Linux).

Since most Linux systems are open source, I suggest you to dive into implementation details by downloading then looking inside the source code of GNU libc or musl-libc: both implement malloc and dlopen above mmap and other syscalls(2).

Windows has similar facilities, but I don't know Windows. Refer to the documentation of the WinAPI

Read also Operating Systems: Three Easy Pieces, and, if you code in C, some good book such as Modern C and some C reference website. Be sure to read the documentation of your C compiler (e.g. GCC). See also the OSDEV website.

Be aware that modern C compilers are permitted to make extensive optimizations. Read what every C programmer should know about undefined behavior. See also this draft report.


In modern systems, a technique called virtual memory is used to give the program its own memory space. Heap and stack locations are at specific locations in virtual memory. The kernel then takes care of mapping memory locations between physical memory and virtual memory. Your program may have a chunk of memory allocated at 0x80000000, but that chunk of memory might be stored in location 0x49BA5400. Your actual RAM stick would be a jumbled mess of chunks of all of those sections in seemingly random locations.