Swift堆栈/堆理解 [英] Swift stack/heap understanding

查看:337
本文介绍了Swift堆栈/堆理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在swift中删除存储在堆栈/堆中的内容。我有一个重要的估计:
你打印的所有内容和内存地址都不是值,它们存储在堆栈中,打印出来的值是什么,这些是堆上的,基本上是根据值和引用类型。我完全错了吗?并且可选地,你能提供堆栈/堆的可视化表示吗?

I want to undesrtand what is stored in the stack/heap in swift. I have a rought estimation: Everything that you print and the memory address appears not the values, those are stored in the stack, and what is printed out as values, those are on the heap, basically according to value and reference types. Am I completely wrong? And optionally, could you provide a visual replresentation of the stack/heap?

推荐答案

如同juul所述参考类型存储在堆和堆中堆栈中的值...

as juul stated Reference types are stored in Heap and values in stack…

这里我想说明原因......

Here i want to give the explanation why…

堆栈和堆?

堆栈用于静态内存分配和堆用于动态内存分配,两者都存储在计算机的RAM中。

Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM .

在堆栈上分配的变量直接存储到存储器中,对该存储器的访问非常快,并且在编译程序时处理它的分配。当函数或方法调用另一个函数,该函数又调用另一个函数等时,所有这些函数的执行将保持挂起,直到最后一个函数返回其值。堆栈始终以LIFO顺序保留,最近保留的块始终是要释放的下一个块。这使得跟踪堆栈非常简单,从堆栈中释放块只不过是调整一个指针。

Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer.

在堆上分配的变量在运行时分配了内存并且访问此内存有点慢,但堆大小仅受虚拟大小的限制记忆。堆的元素彼此之间没有依赖关系,并且可以随时随机访问。您可以随时分配一个块并随时释放它。这使得在任何给定时间跟踪堆的哪些部分被分配或释放变得更加复杂。

Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory . Element of the heap have no dependencies with each other and can always be accessed randomly at any time. You can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.

这篇关于Swift堆栈/堆理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆