The smart pointer Box<T> in rust
Hi everyone, this is my second blog for the smart pointer in rust. We have already taken use of the Box<> in the previous exercises(Like the Christmas tree), and this is a general smart pointer. We will discuss something about this here.
Some general smart pointer: Box<T>,Rc<T>..
String and Vec<T> have some similar characteristics with smart pointer, but not seriously be considered to be smart pointer.
Let’s start with Box<T>.
If you take use of this, your data will be stored onto the heap memory.
Characteristics of Box<T>:
Fixed size: Box<T> itself is a fixed-size pointer that does not increase the size of the value.
Basic use of the Box<T>:


It seems that the value in the box can be access directly, right?
Then, please think back to the Christmas tree exercise, Why do we take use of the Box<>?
In other languages, if we want to implement a LinkedList, or other recursive data structures, that will be easier, we only need to define certain struct or class in our code, and continue our code with these structs or class.
Example of C:
Define of Node in LinkedList:
typedef int ElementType; typedef struct LNode { ElementType data; struct LNode* next; }LNode,*LinkedList;
Construct our LinkedList like this:
LinkedList CreateList1(LinkedList& L) {` LNode* s; int x; L = (LinkedList)malloc(sizeof(LNode)); L->next = NULL; scanf_s("%d", &x); while (x!=9999){ s = (LNode*)malloc(sizeof(LNode)); s->data = x; s->next = L->next; L->next = s; scanf_s("%d", &x); } return L; }
Ok, quite good, but what will happen if we take this think directly into rust? Just construct them recursively.
Code here:

Error happened:

We can clearly see that, the error is: recursive type `List` has infinite size.
It means that we've defined a recursive type—in this case, an enum named List—that directly contains itself without any form of indirection, like a box (Box). Rust needs to know the size of its types at compile time, but a recursively defined type without indirection could theoretically have an infinite size, which is not permissible.
That is the key point of this question, and this is the characteristics of Rust language, we should get to know the size of the Type in compile time! How can we solve it? Rust has provided the help for us: Box<>.
As we said before, Box is a smart pointer, it will allocates space on the heap to store its pointed-to data, here is the List, after we do this, the box will occupy the storage on the heap, the size of Box<T> itself is fixed, then, every time, rust will calculate the size of the Box which is already known, instead of our initial List type, which is unknown. This will solve the problems before.


Everything goes well!
One another question: so, when will the Box be freed?
Let’s have another test:
Code is here:

Actually, I am a little bit surprised when I got this, I initially think the box will live longer and be able to be used out of the scope. It still follows the principle of scope. When it get out of the current scope, the box will be no longer able for use.
I also get to search some resources for the destruction of the Box:
Heap-based data (like Box<T>, String, etc.): When they leave the scope, they release the memory on the heap by implementing the Drop trait's drop method. This requires more resource management logic to ensure memory safety and prevent memory leaks.
But, What about the basic data types (such as i32, f32, etc.)?
Basic types: When
they leave the scope, their destruction process is very straightforward because
they are directly stored on the stack. The space on the stack will be
automatically released when the scope ends, without the need for any special
cleanup actions.
Quite amazing design!

浙公网安备 33010602011771号