✨? Rust Visualized: The Stack, the Heap, and Pointers

The part of your operating system that allocates memory for a program as well as loading its code and data into memory is called a loader.

The loader defines four different areas of memory for a program: code, static, stack, and heap.

The static seg…


This content originally appeared on DEV Community and was authored by ender minyard

The part of your operating system that allocates memory for a program as well as loading its code and data into memory is called a loader.

The loader defines four different areas of memory for a program: code, static, stack, and heap.

The static segment contains global variables, the code segment contains machine code, and the stack segment contains local variables that are defined inside functions.

code snippet

When the function foo goes on the call stack, its stack frame must store foo's arguments, local variables, and return address.

The memory layout of a stack frame is fixed, so the size of variables needs to be known at compile time. The size of variables on the stack cannot grow or shrink.

memory structure

Stacks are a simple but common data structure. Like a pile of pancakes, the last pancakes that go on a stack are the first pancakes to leave the stack. When you add to a stack, you always add to the top.

stack frames

The call stack consists of stack frames, which contain arguments, local variables, and return addresses. Let's look inside the stack frame on the call stack when main is called.

1o

If we look inside the stack frames on the call stack when foo(y) is called, we notice that the arguments, local variables, and variable addresses of foo have been added to the top of the stack.

2o

The heap is dedicated to variables whose size is not known at compile time. You have a lot more freedom with variables stored on the heap, but you lose speed. You can control the lifetime of variables on the heap as well as assign them an arbitrary size.

In Rust, you can allocate memory on the heap using types like Box or Vec.

A Box<T> stores a pointer to heap data on the stack. The type of this Box is type T.

Pointers are an important concept, because we often reference variables in Rust using & syntax. When you reference a variable, you are pointing to that variable's location in memory.

borrowing in rust


This content originally appeared on DEV Community and was authored by ender minyard


Print Share Comment Cite Upload Translate Updates
APA

ender minyard | Sciencx (2021-07-26T02:14:12+00:00) ✨? Rust Visualized: The Stack, the Heap, and Pointers. Retrieved from https://www.scien.cx/2021/07/26/%e2%9c%a8%f0%9f%a5%9e-rust-visualized-the-stack-the-heap-and-pointers/

MLA
" » ✨? Rust Visualized: The Stack, the Heap, and Pointers." ender minyard | Sciencx - Monday July 26, 2021, https://www.scien.cx/2021/07/26/%e2%9c%a8%f0%9f%a5%9e-rust-visualized-the-stack-the-heap-and-pointers/
HARVARD
ender minyard | Sciencx Monday July 26, 2021 » ✨? Rust Visualized: The Stack, the Heap, and Pointers., viewed ,<https://www.scien.cx/2021/07/26/%e2%9c%a8%f0%9f%a5%9e-rust-visualized-the-stack-the-heap-and-pointers/>
VANCOUVER
ender minyard | Sciencx - » ✨? Rust Visualized: The Stack, the Heap, and Pointers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/26/%e2%9c%a8%f0%9f%a5%9e-rust-visualized-the-stack-the-heap-and-pointers/
CHICAGO
" » ✨? Rust Visualized: The Stack, the Heap, and Pointers." ender minyard | Sciencx - Accessed . https://www.scien.cx/2021/07/26/%e2%9c%a8%f0%9f%a5%9e-rust-visualized-the-stack-the-heap-and-pointers/
IEEE
" » ✨? Rust Visualized: The Stack, the Heap, and Pointers." ender minyard | Sciencx [Online]. Available: https://www.scien.cx/2021/07/26/%e2%9c%a8%f0%9f%a5%9e-rust-visualized-the-stack-the-heap-and-pointers/. [Accessed: ]
rf:citation
» ✨? Rust Visualized: The Stack, the Heap, and Pointers | ender minyard | Sciencx | https://www.scien.cx/2021/07/26/%e2%9c%a8%f0%9f%a5%9e-rust-visualized-the-stack-the-heap-and-pointers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.