Exercise – Research Stack and Heap

Please use Google to answer the following questions:

  1. When it comes to memory management, what does it mean that our data are stored on the stack? What are the advantages and disadvantages of using the stack?
  2. When it comes to memory management, what does it mean that our data are stored on the heap? What are the advantages and disadvantages of using the heap?
1 Like

:one: When it comes to memory management, what does it mean that our data are stored on the stack? What are the advantages and disadvantages of using the stack?

The stack allows for the rust compiler to add and remove functions and business logic in a specific order. It means that given a set of rules, the program developer is able to reliably dictate how the story goes within the program. If there was no stack and functions and declarations were fired at random, there would be output inconsistencies and errors all over the place. In short, we’d be dealing with unprecedented levels of bedlam.

For example, let’s say a variable has been declared, that is used in a function:

fn main() {
    let a: u8 = 2;
    printa(a) 
    println!("Let's print a again: {}", a);
}

fn printa(x: u8) {
    println!("Let's print a: {}", x);
    let b: u8 = 4;
}

In this example :arrow_up:, the a variable is added to the stack, and then it hits the printa function, where the printa function is added to the stack, where it goes through the function from top to bottom. Once the last line of the function has been executed, the memory allocated for the function (and the b variable) is removed from the stack, and can continue with the println!("Let's print a again: {}", a); execution. This is a logical set of steps, and the programmer has control over the order in which declarations and methods are processed. It also means that the program no longer has to allocate memory for b after the printa function is removed from the stack.

When it comes to the stack and efficiently managing memory, it is important to note that when a new function is added to the stack, it does not have access to variables outside of what is provided as arguments. This means that the compiler will spit an error if a function calls for a variable and there are no arguments passed, for example:

fn main() {
    let a: u8 = 2;
    printa() 
    println!("Let's print a again: {}", a);
}

fn printa() {
    println!("Let's print a: {}", a);
}

In the example here, a will be unrecognised in the printa function, because it has been added to the stack, and it has no access to variables from below in the stack. Even though a has been declared in the program, the printa function is a new layer on the stack to the main function, and therefore scoping is limited to what is provided within its scope.

The main benefit is that the developer is able to produce super efficient programs which only use the memory which is required for any given call added to the stack. Many developers enjoy the ability to have control over what and when is allocated to memory.

It does however, mean that the developer must be more conscious of what variables are available within the stack, with the help of the compiler. It also means that elements which are stored within the memory can take more time to add and remove.

TDLR:
Advantages: Less/no wasted memory.
Disadvantages: More effort to create and remove elements used within the program… And more headaches trying to please the compiler :stuck_out_tongue:

A really great analogy for understanding the stack (and some other rust concepts) is at this video.

:two: When it comes to memory management, what does it mean that our data are stored on the heap? What are the advantages and disadvantages of using the heap?

The heap allows for global storage for variables. This means that memory allocation can be stored dynamically, and is much less strict than the stack. The heap could be referred to as a free-floating region of memory which is always available.

When looking at a stack, you can visualise it by standing above a stack of papers, and only being able to read what is on the top page.
When looking at a heap, visualise sitting next to a heap of pages sitting on top of each other - you can see all of the sheets, but you can’t read anything directly… but if you want to read something, you can pull the sheet (variable) out that you require and read it, because you can see it. It will just take a little bit more time to pull the sheet out, and put it back in the heap - as opposed to the stack where you can instantly read what’s available on the top of the stack (although nothing below it).

The advantage of using the heap is that the programmer is able to be more loosey goosey :duck: with their code, knowing well that the program will happily draw from the heap. Making variables and using them in functions is more simple and streamlined.

The main disadvantage of this is that programs will generally become inefficient, and especially more so as the program grows in size. The lack of control means that variables will always be available to draw from in storage.

3 Likes
  1. When it comes to memory management, what does it mean that our data are stored on the stack? What are the advantages and disadvantages of using the stack?
    This is a memory management scheme for local variables where data is only accessible within a function. Memory is de-allocated automatically after the function call, all data in the stack end up on contiguous blocks of memory. Advantage is that stack memory is faster, there is less risk of memory leaks, uses less storage. But the data cannot be accessed globally.

  2. When it comes to memory management, what does it mean that our data are stored on the heap? What are the advantages and disadvantages of using the heap?
    Data in the heap-space is referenced from the stack. The advantage is that the data can be accessed globally and is available until the program ends. The disadvantages are that heap memory is slower than stack memory, takes up more space and data leaks can occur when the data is not deleted (which happens automatically with stack memory).

1 Like
  1. Stack is use for variable with fixe length. It’s much quicker to retrieve data or the remove it.
  2. Heap is use for dynamic variable. It need mapping action which cost time.
1 Like

The stack is used for fixed-length variables, and only variables that are passed to a function are visible. But access time is much shorter than on the heap, where global variables and dynamic-length variables live.

1 Like

When it comes to memory management, what does it mean that our data are stored on the stack?

Stack is a linear data structure. A stack is memory used to store temporary (local) variables. Variables are declared, stored and initialized at runtime. When the computing task is complete, the memory of the variable is automatically erased.

  • Stack memory will never become fragmented.

  • Memory is allocated in a contiguous block.

  • Memory allocation is done by compiler instruction and does not require de-allocation of variables.

  • Implemented in 3 ways, simple array based, using dynamic memory, and linked list based.

What are the advantages and disadvantages of using stack?

Advantages of using stack:

  • manage data in a Last In First Out (LIFO) method

  • stores local variables when a function is called, automatically destroyed once returned

  • control how memory is allocated and deallocated

  • automatically cleans up the object

  • not easily corrupted

  • variables cannot be resized

Disadvantages of using stack:

  • stack memory is limited

  • too many objects on the stack can increase the risk of stack overflow

  • random access is not possible

  • variable storage will be overwritten, which can lead to undefined behavior of the function or program

  • the stack will fall outside of the memory area, which might lead to abnormal termination

When it comes to memory management, what does it mean that our data is stored on the heap?

Heap is a hierachical data structure. A heap is memory used to store global variables and supports dynamic memory allocation. The heap is not automatically managed.

  • Heap memory can become fragmented as blocks of memory are first allocated then freed.

  • Memory is allocated in any random order.

  • Memory allocation is manually done by the programmer, explicit de-allocation is required.

  • Implemented using array and trees.

What are the advantages and disadvantages of using the heap?

Advantages of using heap:

  • helps to find greatest and minimum number

  • garbage collection runs on heap to free the memory used by the object

  • used in Priority Queue

  • allows you to access variables globally

  • does not have any limit on memory size

  • variables can be resized

Disadvantages of using heap:

  • heap can provide the maximum memory an OS can provide

  • takes longer to compute

  • memory management is more complicated, as it is used globally

  • takes too much time in execution compared to the stack

1 Like

When it comes to memory management, what does it mean that our data are stored on the stack? What are the advantages and disadvantages of using the stack?

Data piles up like plates in memory according to the called function which declares them in its life cycle, besides stack has First in First Out rule, data gets stored in the call order with a size limit. That deterministic status of stack makes it faster.

  • The stack is very fast
  • Default initial memory allocation place (stack frame)
  • Limited in size
  • When the functions exist, memory gets dellocated automatically

When it comes to memory management, what does it mean that our data are stored on the heap? What are the advantages and disadvantages of using the heap?

Data gets stored randomly located, without size limit, heap is like unresponsible usage of memory and with this it is slower but global. Heap is getting allocated by Box type allocates memory as boxes. The actual value of the box is a structure which has a pointer to ‘the heap’ as a reference. First value which declared to a variable as Box type has a value as whole memory’s -physically mounted to the device- length -1, in an example of 1GB RAM memory it is (2^30)-1.

  • Slower
  • Explicitly allocated by the program
  • Effectively unlimited in size
  • Globally accessible data storage
Resource: The Rust Programming Language, Effective Rust, 4.1. The Stack and the Heap, MIT
1 Like
  1. It means is located in the memory. Its advantages are that it takes less resources and its faster to read.
  2. It means that is located in our programm . Unlike the memory the size is unlimited.
1 Like
  1. with stack we mean an array of byte where Rust storage variable data, this storage è more speed then heap, but it save only local data call in a function.
    The advance of this method is that programm can allocate and deallocate data in simultany operation.

2.witrh heap, rust allocate variable in memory starting to end byte’s aarray memory.
But ad different of the Stack , this allocation can be , deallocate in all orders.Variable save in heap not finish at end function, but rest in heap if not dealocate resorce o programm end…

1 Like
1. When it comes to memory management, what does it mean that our data are stored on the stack? What are the advantages and disadvantages of using the stack?
  • Memory is managed by the runtime
  • Scoped to the current function in the call stack… all memory allocated within the function is only visible to that function and is garbage collected after execution of the that function finishes
  • Thread safe
  • Small amount
  • Simpler
2. When it comes to memory management, what does it mean that our data are stored on the heap? What are the advantages and disadvantages of using the heap?
  • Manually managed by developer’s code… which can lead to memory leaks if not cleaned up properly
  • Global scope… not thread safe as visible to all threads
  • Can become fragmented
  • More overhead to access heap than stack
  • Larger amount available
1 Like

Please use Google to answer the following questions:

1. When it comes to memory management, what does it mean that our data are stored on the stack? What are the advantages and disadvantages of using the stack?
The stack is for fixed length data types - meaning that the size of the variable is known at compile time. Data stored on the stack has the advantage of better retrievability because the size is of the data is small. A local variable can be allocated to the stack memory during the function but when a it is returned at the end of the function program then it will be automatically deallocate which is an advantage of the stack model.
2. When it comes to memory management, what does it mean that our data are stored on the heap? What are the advantages and disadvantages of using the heap?
The heap sector of the memory model of rust is dynamic data that is not known at compile time. For this reason the heap has a bigger memory capacity (advantage over stack), however, the disadvantage of this is that the data takes longer to be retrieved and the data that is referenced has to removed/dereferenced manually after the lifetime of the program

1 Like

When it comes to memory management, what does it mean that our data are stored on the stack?
What are the advantages and disadvantages of using the stack?

Stack memory management stores temp variables created by a function in a computers memory.
some advantages of stack are: manage data in last in first out (LIFO), automatically destroys function when called,
control over how memory is allocated and deallocated.

some disadvantages are a very limited memory, risk of stack overflow when too many objects are create,
undefined behaviors when variable storage is overwritten

When it comes to memory management, what does it mean that our data are stored on the heap?
What are the advantages and disadvantages of using the heap?
heap means memory is used by the programming language to store global variables for dynamic memory allocation support,
also global variables are stored in heap by default.

Heap helps in finding greatest and minimum number, garbage collection to free memory from objects, priority queue
allows access to global variables

some heap disadvantages are longer compute times, complicated memory management when used globally,
slow execution compared to stack.

1 Like

The stack is very fast, and is where memory is allocated in Rust by default. But the allocation is local to a function call, and is limited in size. The heap, on the other hand, is slower, and is explicitly allocated by your program. But it’s effectively unlimited in size, and is globally accessible.

1 Like
  1. Data stores on the stack are on a special space in memory initialized during runtime. The advantage is the data will erase itself after the return of the function.

  2. Heap is a free floating space in memory to store global variable. The advantage is it allows dynamic memory allocation. The disadvantage is it has to be managed by the programmer instead of the CPU.

1 Like
  1. Stack is used for fixed length variables. Data gets stored in the call order with a size limit. This makes it very faster. When a function exist, memory gets deallocated automatically. Some disadvantages may include, default initial memory allocation place, and limited in size.

  2. In heap, data gets stored randomly, without size limit. This makes memory management slower, but global. Some advantages may be that it is explicitly allocated by the program, effectively unlimited in size, and has globally accessible data storage.

Please use Google to answer the following questions:

  1. When it comes to memory management, what does it mean that our data are stored on the stack? What are the advantages and disadvantages of using the stack?
  • A stack is a special area of computer’s memory which stores temporary variables created by a function. In stack, variables are declared, stored and initialized during runtime.

  • Advantages

  1. Helps you to manage the data in a Last In First Out(LIFO) method which is not possible with Linked list and array.
  2. When a function is called the local variables are stored in a stack, and it is automatically destroyed once returned.
  3. A stack is used when a variable is not used outside that function.
  4. It allows you to control how memory is allocated and deallocated.
  5. Stack automatically cleans up the object.
  6. Not easily corrupted
  7. Variables cannot be resized.
  • Disadvantages
  1. Stack memory is very limited.
  2. Creating too many objects on the stack can increase the risk of stack overflow.
  3. Random access is not possible.
  4. Variable storage will be overwritten, which sometimes leads to undefined behavior of the function or program.
  5. The stack will fall outside of the memory area, which might lead to an abnormal termination.
  1. When it comes to memory management, what does it mean that our data are stored on the heap? What are the advantages and disadvantages of using 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. It is more like a free-floating region of memory.

  • Advantages

  1. Heap helps you to find the greatest and minimum number
  2. Garbage collection runs on the heap memory to free the memory used by the object.
  3. Heap method also used in the Priority Queue.
  4. It allows you to access variables globally.
  5. Heap doesn’t have any limit on memory size.
  • Disadvantages
  1. It can provide the maximum memory an OS can provide
  2. It takes more time to compute.
  3. Memory management is more complicated in heap memory as it is used globally.
  4. It takes too much time in execution compared to the stack.
1 Like

https://academy.moralis.io/quizzes/quiz-rust-fundamentals

Hi, This quiz is totally different than what has been taught in Rust. Can you please double check, may be its a mistake?

The stack is a type of memory on the basis of Last Input First Output (LIFO). For memory management usually this is more efficient and secure. The stack allocation happens in the function call and when the function process is over, the memory is de-allocated. This is a temporary allocation scheme which makes the memory management very simple and efficient. Also this is a very secure memory since its only available for the owner thread. The allocation and de-allocation is very fast compared to the Heap memory. The con is that since it doesn’t require much memory because most of it is temporary memory, the stackoverflow is a quite common situation when programmers don’t take into account… specially in recursive function calling.

The HEAP memory is for the program execution to store at its will. The amount won’t be known until the program is running. For example, if the execution creates an object, this is stored in the heap, while its refference in the stack. For this reason, the heap memory is a lot larger than the stack. Its management is more complex and requires the programmer to take special care. High level programming languages such as Python and Javascript does it automatically so the programmer is liberated from this task. These languages have “garbage collectors”. While these guys make the programmers life much easier, high level programming languages are way more slower than low level ones such as C++ at execution. Another problem with the heap is that since it’s not controlled and owned by a particular function, are prone to memory leaks.