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?

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

2 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