Search
 
SCRIPT & CODE EXAMPLE
 

RUST

armanriazi•rust•stack•vs•heap

     {
     Rust programs have 3 memory regions where data is stored:

	 #data memory - For data that is fixed in size and static (i.e. always available through life of program). Consider the text in your program (e.g. "Hello World!"): This text's bytes are only ever read from one place and therefore can be stored in this region. Compilers make lots of optimizations with this kind of data, and they are generally considered very fast to use since locations are known and fixed.
	
	 #stack memory - For data that is declared as variables within a function. The location of this memory never changes for the duration of a function call; because of this compilers can optimize code so stack data is very fast to access.

	 #heap memory - For data that is created while the application is running. Data in this region may be added, moved, removed, resized, etc. Because of its dynamic nature it's generally considered slower to use, but it allows for much more creative usages of memory. When data is added to this region we call it an allocation. When data is removed from this section we call it a deallocation.
     }
     {
     e.g. let s= Struct{x:y,z:w}
     String struct is also on stack,    
     but holds a reference to data on heap
     }
     
{
Data with an unknown size at compile time or a size that might change must be stored on the heap instead.

The heap is less organized: when you put data on the heap, you request a certain amount of space. The memory allocator finds an empty spot in the heap that is big enough, marks it as being in use, and returns a pointer, which is the address of that location. This process is called allocating on the heap and is sometimes abbreviated as just allocating. Pushing values onto the stack is not considered allocating. Because the pointer to the heap is a known, fixed size, you can store the pointer on the stack, but when you want the actual data, you must follow the pointer. Think of being seated at a restaurant. When you enter, you state the number of people in your group, and the staff finds an empty table that fits everyone and leads you there. If someone in your group comes late, they can ask where you’ve been seated to find you.

Pushing to the stack is faster than allocating on the heap because the allocator never has to search for a place to store new data; that location is always at the top of the stack. Comparatively, allocating space on the heap requires more work, because the allocator must first find a big enough space to hold the data and then perform bookkeeping to prepare for the next allocation.
}
     
     
Comment

PREVIOUS NEXT
Code Example
Rust :: rust match wildcard 
Rust :: armanriazi•rust•type•recursive 
Rust :: rust print i8 
Rust :: rust-analyzer tab space 
Rust :: Create and populate a 2d vector 
Rust :: rustdoc 
Rust :: rust•armanriazi•slice•vs•char•vec 
Rust :: armanriazi•rust•thread•channel 
Rust :: armanriazi•rust•smartpointer•box•vs•rc•vs•refcell 
Rust :: rustlang get substring 
Lua :: roblox make rainbow part 
Lua :: lua multiline comment 
Lua :: lua pcall 
Lua :: luau debounce 
Lua :: roblox lua gui drag 
Lua :: roblox studio part color randomiser 
Lua :: roblox what is humanoid 
Lua :: lua json 
Lua :: how to print in lua 
Lua :: convert string to lowercase lua 
Lua :: convert number to string lua 
Lua :: attempt to call a string value lua 
Lua :: insert item array pico8 
Lua :: roblox for loop 
Matlab :: matlab symbolic function 
Matlab :: print hello world n times in matlab 
Basic :: how to open d drive using conda prompt 
Basic :: fill in the commands belllow if you wanted to map a value saved in the variable count from one tange to another 
Elixir :: elixir read csv file 
Scala :: scala hello world 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =