[Seed7-users] freeing of dynamically allocated memory in seed7
Interpreter and compiler for the Seed7 programming language.
Brought to you by:
thomas_mertes
From: Thomas M. <tho...@gm...> - 2012-09-11 22:26:34
|
On 10 Sep 2012 11:19:38 +0200, joh...@so... wrote: > hi, > > i was looking at the seed7 language and am quite impressed with quite a > few things. Thank you. > however, there is something bothers me, because i found no answer or hint > anywhere. how do you free dynamically allocated memory? arrays and > structures defined inside a function are usually released when that function exits, > i found out this much. Yes, strings, arrays, hashes, structs and other structured data types manage their memory. E.g.: When an element is removed from a hash table the memory used by the element is freed as well as hash table internal data. And when a structured data type is freed all the data inside it is also freed. Intermediate results of expressions (which also can be structured data) are freed after they were used. I recently improved the FAQ answer, regarding garbage collection, a little bit. See: http://seed7.sourceforge.net/faq.htm#garbage_collection > but what if you have a ptr to something, Many languages rely heavily on pointers, respectively references. This either leads to explicit memory freeing or to garbage collection processes. In Seed7 pointers are not so heavily used. Seed7 encourages structured programming, which refers to structured statements and structured data types. A goto is not a structured statement. Statements like if, while, repeat, for, etc. should be used instead. A pointer is not a structured data type. Instead containers like array, hash, set, etc. should be used. As I said before, the structured data types manage their memory. This structured data approach allows a simple automatic memory management, which covers more than 99.9% of the data. For the remaining 0.1% of the data a different approach must be used. > or use the xalloc() function, or > store multiple references to an object? The interface types of Seed7 have this problem. An interface type cannot use the simple automatic memory management. > do you use something like reference > counting or is the allocated memory just never freed? Currently the memory is not freed, but it is planned to use reference counting. Sorry to disappoint you. I hope that this does not scare you off. Since pointers should be replaced by structured data types the interface and implementation types of the object orientation are the only types, which currently don't have managed memory. OO is used heavily in Seed7, but not to define container classes. Containers are defined with abstract data types instead. So you don't have trees, single or double linked lists, etc. In practice the memory leaks caused by interface types are much smaller than 0.1%. Because of the container types it is often not necessary to create new objects all the time. Many of the example programs don't have any leaks. If you have other questions, just ask. Regards, Thomas Mertes |