简体   繁体   English

MIPS - 释放动态分配的空间(堆)

[英]MIPS - free dynamically allocated space (heap)

I am trying to free dynamically allocated heap memory space我正在尝试释放动态分配的堆 memory 空间

I've tried running this simple code but I keep getting the same error:我试过运行这个简单的代码,但我一直收到同样的错误:

"request (-40) is negative heap amount (syscall 9)" “请求(-40)是负堆量(系统调用 9)”

.text
li $v0,9
li $a0,40
syscall

li $v0,9
li $a0,-40
syscall

Can anyone help me understand why it is not working?谁能帮我理解为什么它不起作用? Is there a different way to free space?有没有不同的方法来释放空间?

MARS doesn't support sbrk with negative arguments, as you can see in the source code here . MARS 不支持负 arguments 的sbrk ,如您在此处的源代码中所见。

So as far as I can tell there's no way to explicitly free dynamically allocated memory when running your code in MARS.据我所知,在 MARS 中运行代码时,无法显式释放动态分配的 memory。

If you free heap memory by using a negative sbrk that basically turns the heap into a stack, as only memory at the end can be returned — that is not really what the heap is about.如果您通过使用负sbrk来释放堆 memory,这基本上会将堆变成堆栈,因为最后只能返回 memory - 这并不是堆的真正含义。 In the heap we want to be able to free any arbitrary object, not solely the end.在堆中,我们希望能够释放任意 object,而不仅仅是结束。

So, the questions are:所以,问题是:

  • Why do you need to release heap memory?为什么需要释放堆memory?
  • Are you really ok using the heap like a stack?你真的可以像堆栈一样使用堆吗?

You can implement malloc & free .您可以实现malloc & free Though these are usually quite complicated due to their general purpose nature and performance considerations, a simple, slow one is pretty easy to write — especially if you have knowledge of your application and its limited requirements.尽管由于它们的通用性和性能考虑,这些通常非常复杂,但是一个简单、缓慢的代码很容易编写——尤其是如果您了解您的应用程序及其有限的要求。

A truly general purpose malloc / free uses a data structure that collects the free blocks.真正通用的malloc / free使用收集空闲块的数据结构。 malloc first searches that to satisfy a request, hands out an existing free block or else uses sbrk. malloc首先搜索满足请求,分发现有空闲块或使用 sbrk。 free returns a block to the collection. free向集合返回一个块。 (There's more to it of course: splitting large blocks, merging free blocks...) (当然还有更多:分割大块,合并空闲块......)

However, if you were ok using the heap like a stack, you could maintain two pointers, one indicating where free memory starts, and another where it ends.但是,如果您可以像堆栈一样使用堆,您可以维护两个指针,一个指示空闲 memory 的开始位置,另一个指示它结束的位置。 Releasing memory most recently allocated (like sbrk(-400) ) would decrement the free memory start pointer by 400. Allocating memory would increment free memory start by the desired amount, while also moving the free memory end pointer if needed (using a sbrk of whatever size is still needed). Releasing memory most recently allocated (like sbrk(-400) ) would decrement the free memory start pointer by 400. Allocating memory would increment free memory start by the desired amount, while also moving the free memory end pointer if needed (using a sbrk of仍然需要任何尺寸)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM