首页 > 其他分享 >The Stack and the Heap栈与堆__Rust

The Stack and the Heap栈与堆__Rust

时间:2024-04-21 19:44:42浏览次数:25  
标签:__ 数据 stack 内存 heap allocating data Stack Rust

Many programming languages don’t require you to think about the stack and the heap very often. 许多编程语言并不会要求你经常思考堆栈。But in a system programming language like Rust, whether a value is on the stack or the heap affects how the language behaves and why you have to make certain decisions. 但是对于Rust这样的系统级编程语言来说,值存储在栈或是堆中将直接影响语言的行为以及为什么必须做出某些确切的决定。Parts of ownership will be described in relation on the stack and the heap later in this chapter, so here is a brief explanation in preparation.由于Rust中的所有权与其存在关联,因此这是一个开胃菜(准备)的解释。

 

Both the stack and the heap are parts of memory available to your code to use at runtime, but they are structured in different ways.栈与堆都是你代码在运行时所使用的内存。但是二者之间的结构存在一定不同之处。 The stack stores values in the order it gets them and removes the values in the opposite order. 栈空间存储值与释放值空间的顺序相反。This is referred to as last in, first out. 被称为后进先出。Think of a stack of plates: when you add more plates, you put them on top of the pile, and when you need a plate, you take one off the top.这里有一叠盘子,堆放时只能从最上方堆叠,拿取时只能从最上方移除。 Adding or removing plates from the middle or bottom wouldn’t work as well! 从中间或底部来堆叠或移除盘子都是不恰当的!Adding data is called pushing onto the stack, and removing data is called popping off the stack.添加数据称为入栈,移除数据称为出栈。 All data stored on the stack must have a known, fixed size. 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, mark 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).这一过程被称为堆上分配,有时会简写为allocating(入栈不被视为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 restaurant. When you enter, you state the number of people in your group, and the host 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.相较而言,堆上分配需要做更多工作,因为内存分配器需要先寻找一个空余的、足够的内存空间来储存数据,并记录,为下一次分配做准备。

 

Accessing data in the heap is slower than accessing data on the stack because you have to follow a pointer to get there. 在堆中访问数据比在栈中访问数据更慢。Contemporary processors are faster if they jump around less in memory.对于现代处理器而言跳转的内存区域越小执行速度越快。 Continuing the analogy, consider a server at a restaurant taking orders from many tables. 继续类比,一名餐厅服务员给许多桌客人点餐。It’s most efficient to get all the orders at one table before moving on the next table.最有效率(快)的方式就是一桌接着一桌来点餐。 Taking an order from table A, then an order from table B, then one from table A again, and then one from B again would be a much slower process.若是点餐顺序从A到B再到A再到B,将会是更慢的过程。 By the same token, a processor can do its job better if it works on data that’s close to other data(as it is on the stack) rather than farther away(as it can be on the heap).同样道理,处理器在数据之间(内存距离)更近的条件下执行代码时会表现更好。

 

When your code calls a function, the values passed into the function(including, potentially, pointers to data on the heap) and the function’s local variables get pushed onto the stack. When the function is over, those values get popped off the stack.当你的代码调用函数时,传递给函数的值(可能包括指向堆上数据的指针)和函数的局部变量入栈,当函数结束时,这些值出栈。

 

Keeping track of what parts of code are using what data on the heap, minimizing the amount of duplicate data on the heap, and cleaning up unused data on the heap so you won’t run out of space are all problems that ownership addresses. 跟踪代码的哪些部分正在使用堆上的哪些数据,最大限度地减少堆上的重复数据量,以及清理堆上未使用的数据,这样就不会耗尽空间,这些都是所有权解决的问题。Once you understand ownership, you won’t need to think about the stack and the heap very often, but knowing that the main purpose of ownership is to manage heap data can help explain why it works the way it dose.一旦理解了所有权,您就不需要经常考虑堆栈和堆,但是知道所有权的主要目的是管理堆数据可以帮助解释为什么它会这样工作。

标签:__,数据,stack,内存,heap,allocating,data,Stack,Rust
From: https://www.cnblogs.com/ashet/p/18149387

相关文章

  • 3月30日成功举办——基于实在智能RPA的微信私域自动化运营公开课
    3月30日,RPA学习天地成功举办了,基于微信私域运营的RPA自动化直播公开课,成功地帮助学员们普及了社交化工具私域运营的自动化技巧。 本次公开课,主要介绍了如何使用实在智能RPA工具来自动化实现微信的好友添加,建群,群发信息等私域互动信息,受到了观众的好评。以下为公开课部分精彩......
  • 7.整数反转
    题目:给一个32位有号整数,要求将整数反转得到新整数,若原整数不在[-2^31,2^31-1]范围,则返回0。Python理论上可以存储无穷大的数,因此题目的32位即[-2^31,2^31-1]只需Python作出范围的判断不用更换类型。反转的思路:进行类型的转换——将数转换成字符串类型,问题等价于反转字符串,按......
  • 前端定义一个对象的时候,key值如何设置为数字,并抽成常量
    在JavaScript中,如果你想将对象的键值设置为常量数值,首先需要定义这些常量。然后你可以在创建对象时使用这些常量作为键。例如://定义常量constKEY_ONE=1;constKEY_TWO=2;constKEY_THREE=3;//使用常量作为对象的键letmyObject={[KEY_ONE]:'one',[KEY_T......
  • 关于题目集1~3的总结
    前言前三次pta作业最后一题都是答题判题程序,题目难度逐级提升但写完后收获也不小。首先一点是需求分析,不应上来就写代码而是从业务需求整体分析,在确定好程序的层次结构再开始实现相应的功能。在这三次作业中,将所学的编程知识很好地运用,其次,三次作业也同样考验我们的自学能力比......
  • NCHU题目集1~3的总结
    目录一.前言二.设计与分析三.采坑心得四.改进建议五.总结前言知识点总结题目集一字符串处理对输入的字符串进行对应的匹配与确认,找到对应的字符串的内容,将对应字符串中不合规范的内容进行处理,合格后直接存储。字符串的比对,满足要求的字符串进行输出和相应......
  • 答题判题程序
    答题判题程序题目要求设计实现答题程序,模拟一个小型的测试,要求输入题目信息、试卷信息、答题信息、学生信息、删除题目信息,根据输入题目信息中的标准答案判断答题的结果。详见:答题判题程序前言前两次作业较为的输入较为简单,第一次还不支持乱序输入.但第三次作业的难度就......
  • 树状数组入门
    树状数组下标记得是从1开始,本节点id通过加lowbit可以访问到父节点的id,用于点修。本节点id减去lowbit则是查看左边第一个比自己高一级的节点id,比如7会查到6,6会查到4,这样子累加此三个的值就可以得到前七个的前缀和。inttreeArr[M]={0};//startfrom1intlowbit(intx){......
  • 微信登录+商品浏览
    先解决了图片不能回显的问题:以前在学习springboot3+vue3的时候当时的配置是  这样配置可以上传图片。然后学外卖的时候还用这个配置,发现不能图片不能回显了,把选中部分删了就可以图片回显了,我只能说,抽象。   为什么要使用HttpClient,因为在实现微信登录的过程中呢,需......
  • 简述:将带透明通道的PNG图片叠加到Framebuf上
    PNG图片,维基百科简介:https://zh.wikipedia.org/wiki/PNG现在带透明通道的最常见的PNG文件都是PNG32格式,所以首先将PNG文件解析为RGBA-8-8-8-8即32位真彩像素(PNG32)(A代表Alpha透明通道)PNG帧解析PNG文件编码比BMP复杂一点,但还是可以解析的。在no_stdrust上我就偷懒使用了mi......
  • windows命令行命令
    windows命令行命令一般在cmd或bat脚本中使用常见命令dir:显示当前目录中的文件和子目录列表。cd:更改当前目录。mkdir:创建一个新目录。del:删除文件。copy:复制文件。move:移动文件或更改文件名。ren:重命名文件。type:显示文本文件的内容。ping:测试与另一......