I am really confused about Rust's system of memory allocation.
In Java you use new
to allocate memory on the heap. In C you use malloc()
, everything else goes on the stack.
I thought, in Rust, Box<T>
allocates memory on the heap but after reading "Defining Our Own Smart Pointer" section in chapter 15.2 in The Rust Programming Language it seems like MyBox<T>
doesn't have any special annotation to make the value of T
live on the heap.
What exactly goes on the stack and what goes on the heap?
Is the implementation of MyBox<T>
essentially the same as Box<T>
?
If the implementations are identical, what makes T
stored on the heap rather than the stack?
If the implementations aren't identical what makes a Box<T>
allocate memory on the heap?
The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation. The heap is not managed automatically for you and is not as tightly managed by the CPU.
These two terms are about memory management. The stack and the heap are abstractions that help you determine when to allocate and deallocate memory. Here's a high-level comparison: The stack is very fast, and is where memory is allocated in Rust by default.
Usually Rust avoid allocating anything on the heap. Never will the compiler do an implicit allocation on the heap, but may library functions can do it for you. At least anything that is dynamically sized (eg. Vec<T> ) will need something on the heap under the hood, for the rest, the documentation should hint it.
Rust doesn't have a defined memory model in the language specifications as of now and the memory structure is quite straightforward. Each Rust program process is allocated some virtual memory by the Operating System(OS), this is the total memory that the process has access to.
This is hard to say. Usually Rust avoid allocating anything on the heap. Never will the compiler do an implicit allocation on the heap, but may library functions can do it for you. At least anything that is dynamically sized (eg. Vec<T>
) will need something on the heap under the hood, for the rest, the documentation should hint it.
Note that even in C, many functions can do heap allocation without an explicit call to malloc
. Eg. I've had to debug a memory leak recently where a developer called getaddrinfo
without a corresponding freeaddrinfo
, ignoring that this function allocates memory on the heap. This class of bugs should be really rare in Rust however, thanks to RAII.
Not at all! The book is simplifying things here to avoid details not important for this section.
—
Box
is a compiler built-in. Under the hood what allocates the memory is an allocator defined as in liballoc
. You can think of this allocator as providing malloc
-like functionality. In practice, the default allocator uses jemalloc on most targets, it is also possible to use a custom allocator, for example the alloc_system
crate uses the system's malloc
/realloc
/free
functions to build its allocator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With