Do I need to Box child structs of a Boxed struct to get everything on the heap?

瘦欲@ 提交于 2020-04-30 15:44:15

问题


I don't understand in Rust what is happening with a struct in a struct when we Box the parent struct.

struct Outer1 {
    child: Inner1,
}

struct Inner1 {
    n: i32,
}

struct Outer2 {
    child: Box<Inner2>,
}

struct Inner2 {
    n: Box<i32>,
}

pub fn main() {
    let x1 = Box::new(Outer1 {
        child: Inner1 { n: 1 },
    });
    let x2 = Box::new(Outer2 {
        child: Box::new(Inner2 { n: Box::new(1) }),
    });
}

x2.child and x2.child.n should be on the heap, right? Where is x1.child and x1.child.n: the stack or the heap?

If child.n would be of type String, n should be a reference and String needs no Box to be on the heap? Is this correct?


回答1:


Imagine each type (Outer1, Inner1, i32) has a color and that memory slots are made up of one or more stacked/embedded colored frames of bytes.

struct Outer1 {
    child: Inner1,
}

struct Inner1 {
    n: i32,
}

let x1 = Box::new(Outer1 {
    child: Inner1 { n: 1 },
});

has this layout:

struct Outer2 {
    child: Box<Inner2>,
}

struct Inner2 {
    n: Box<i32>,
}

let x2 = Box::new(Outer2 {
    child: Box::new(Inner2 { n: Box::new(1) }),
});

has this layout:

Each Box<Something> start a new colored frame.

Obviously memory is not colored: it is a concept that may help in representation: what really exists are only bytes, and eventually some waste of space due to memory alignments.

Below the memory layout for a struct containig a String property.

A String is made up of three components: a pointer to a buffer, lenght and capacity (in the example they are on the stack) and a buffer that is always stored on the heap.

Note: it the examples above pointers size are relative to 64 bit architecture.




回答2:


Do I need to Box child structs of a Boxed struct to get everything on the heap?

No. When you box a value, you box the entire value, which means all of the member values. Since every member of that value is inside the box, everything is on the heap.

What happens if I do inner boxing? Does it point to different memory?

Yes. The variable on the stack will point to data in the heap which will then point at different data on the heap.

+-----------+         +----------+
|   Stack   |         |   Heap   |
+-----------+         +----------+
|           +--------->          +------+
|           |         |          |      |
|           |         |          |      |
|           |         |          |      |
+-----------+         |          |      |
|   .....   |         +----------+      |
+-----------+         |   ....   |      |
|           |         +----------+      |
|           |         |          |      |
|           |         |          <------+
|           |         |          |
|           |         |          |
|           |         |          |
|           |         |          |
|           |         |          |
+-----------+         +----------+


来源:https://stackoverflow.com/questions/52927610/do-i-need-to-box-child-structs-of-a-boxed-struct-to-get-everything-on-the-heap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!