释放先前分配给变量的内存(使用create)(Deallocate memory previously allocated to a variable (using create))

编程入门 行业动态 更新时间:2024-10-23 19:22:25
释放先前分配给变量的内存(使用create)(Deallocate memory previously allocated to a variable (using create))

我正在阅读有关内存分配/释放的Gforth手册,这是我无法理解的。 假设我分配了一块内存来保存四个整数,如下所示:

create foo 1 , 2 , 3 , 4 ,

然后,也许我分配了更多内存,也许还释放了一些内存,现在我想解除分配foo 。 我怎么做? foo free执行foo free和foo 4 cells free导致错误。

I'm reading the Gforth manual on memory allocation / deallocation, and this is something I cannot understand. Suppose I allocated a chunk of memory to hold four integers like this:

create foo 1 , 2 , 3 , 4 ,

Then, maybe I allocated more memory and perhaps deallocated some too, and now I want to deallocate foo. How do I do that? Doing foo free and foo 4 cells free results in an error.

最满意答案

一种选择是使用forget foo但是这将“释放”自你定义foo以来定义的所有内容,并且比Gforth没有实现它更糟糕。 在Gforth你必须使用'标记',但这也将恢复标记后发生的所有事情。

例如(我将展示你将把它输入到Gforth解释器中,包括解释器的响应(用双星号表示)):

marker -unfoo **ok** create foo 1 , 2 , 3 , 4 , **ok** / A test word to get the first thing in foo (1) back : test foo @ . ; **ok** test **1 ok** -unfoo **ok** foo **:8: Undefined word >>>foo<<< Backtrace: $7FAA4EB4 throw $7FAB1628 no.extensions $7FAA502C interpreter-notfound1** test **:8: Undefined word >>>test<<< Backtrace: $7FAA4EB4 throw $7FAB1628 no.extensions $7FAA502C interpreter-notfound1**

该示例旨在说明执行-unfoo后foo和test都消失了。

这实际上是如何工作的可能是我移动解释器正在采取的地址作为最后添加到字典中的东西。 -unfoo将其移回到添加foo的地址之前,这相当于释放foo使用的内存。

这是这个Starting Forth的另一个参考,非常适合一般的Forth。


回应对此答案的评论:

这个问题非常相似, 这个答案非常有帮助。 这可能是Gforth文档中最相关的部分。

上面的链接解释了malloc() , free()和resize() Forth版本。

因此,在回答原始问题时,您可以free使用,但您必须通过allocate或resize来allocate您自由的内存。

create会在字典中添加一个项目,如果你想要恢复内存,那么就不是你想要的。 我对此的理解可能是不正确的,因为在正常执行过程中你通常不会从字典中删除东西。

存储字符串的最佳方法取决于您要对其执行的操作。 如果你不需要它在程序的生命周期中存在,你可以单独使用s" ,因为它返回一个长度和一个地址。

总的来说,我会说使用create是一个很好的主意但它确实有局限性。 如果字符串更改,则必须为其create新的字典条目。 如果你可以设置字符串长度的上限,那么一旦你create da word,你就可以返回并覆盖为它allot的内存。

这是我给出的另一个答案,给出了定义字符串单词的示例。

总而言之,如果你确实需要能够释放内存,请使用Gforth提供的堆方法(我认为它们符合Forth标准,但我不知道是否所有Forth都实现了它们)。 如果不这样做,您可以根据您的问题使用字典。

One option is to use forget foo but that will 'deallocate' everything that you have defined since you defined foo, and worse than that Gforth doesn't implement it. In Gforth you have to use a 'marker', but this also will revert everything that happened after the marker.

For example (I'll show what you would get entering this into a Gforth interpreter, including the interpreter's responses (denoted by double asterisks)):

marker -unfoo **ok** create foo 1 , 2 , 3 , 4 , **ok** / A test word to get the first thing in foo (1) back : test foo @ . ; **ok** test **1 ok** -unfoo **ok** foo **:8: Undefined word >>>foo<<< Backtrace: $7FAA4EB4 throw $7FAB1628 no.extensions $7FAA502C interpreter-notfound1** test **:8: Undefined word >>>test<<< Backtrace: $7FAA4EB4 throw $7FAB1628 no.extensions $7FAA502C interpreter-notfound1**

The example is meant to illustrate that foo and test are both gone after you execute -unfoo.

How this actually works is probably my moving the address that the interpreter is taking as the last thing added to the dictionary. -unfoo moves this back to before the address at which foo was added, which is equivalent to freeing the memory used by foo.

Here is another reference for this Starting Forth which is pretty excellent for picking up Forth in general.


In response to a comment on this answer:

This question is quite similar and this answer is pretty helpful. This is probably the most relevant part of the Gforth documentation.

The links above explain Forth versions of malloc(), free() and resize().

So in answer to your original question, you can use free but the memory that you free has to have been allocated by allocate or resize.

create adds an item to the dictionary and is as such not exactly what you want if you are going to want the memory back. My understanding of this, which may be incorrect is that you wouldn't normally remove things from the dictionary during the course of normal execution.

The best way to store a string depends on what you want to do with it. If you don't need it to exist for the lifetime of the programme you can just use s" by itself as this returns a length and an address.

In general, I would say that using create is quite a good idea but it does have limitations. If the string changes you will have to create a new dictionary entry for it. If you can set an upper bound on the string length, then once you have created a word you can go back and overwrite the memory that has been alloted for it.

This is another answer that I gave that gives an example of defining a string word.

So in summary, if you really do need to be able to deallocate the memory, use heap methods that Gforth provides (I think that they are in the Forth standard but I don't know if all Forths implement them). If you don't you can use the dictionary as per your question.

更多推荐

本文发布于:2023-08-07 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   先前   内存   create   Deallocate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!