You cannot only use a stack, because a stack requires a last-in first-out allocation & deallocation order (i.e. you can only deallocate the newest allocated data; in a stack you cannot deallocate some old data and keep some newer one).
Actually, you can get rid of the stack (only keeping the heap). See Appel's paper Garbage Collection Can Be Faster Than Stack Allocation and his Compiling with Continuation book.
And heap does not have a well defined meaning (other than "dynamically allocated memory which is not on the stack"). Actually, on Linux systems, allocating a big chunk of memory using the mmap system call is fairly quick (but malloc
implementations try to avoid mmap
and prefer reusing free
-d memory). The issue is allocation of small memory zones.
And read more about garbage collection techniques. In C or C++ you might use Boehm's GC
A stack is often useful, notably for recursive function calls. It is so useful (e.g. in C) that today's processors usually have a dedicated stack pointer register (used by CALL & RET machine instructions for calling & returning). But this was not always the case; on some processors (eg IBM360), the stack pointer is a conventional register, not a hardcoded one.
See also this & that answers (and other ones) about virtual address space.