Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What goes on the stack and what goes on the heap in Rust?

Tags:

rust

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.

  1. What exactly goes on the stack and what goes on the heap?

  2. Is the implementation of MyBox<T> essentially the same as Box<T>?

  3. If the implementations are identical, what makes T stored on the heap rather than the stack?

  4. If the implementations aren't identical what makes a Box<T> allocate memory on the heap?

like image 731
Jim Morrison Avatar asked Oct 02 '18 20:10

Jim Morrison


People also ask

What goes 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.

What is heap in Rust?

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.

Does Rust use heap?

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.

How does Rust allocate memory?

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.


1 Answers

  1. 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.

  2. Not at all! The book is simplifying things here to avoid details not important for this section.

  3. 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.

like image 142
mcarton Avatar answered Sep 29 '22 18:09

mcarton