From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c233446a6027f1ed X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-07-16 03:54:05 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!194.25.134.62!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.freenet.de!not-for-mail From: Jan Prazak Subject: Re: access / freeing memory Date: Tue, 16 Jul 2002 12:54:40 -0100 Newsgroups: comp.lang.ada Message-ID: References: <%WHY8.1173$tt5.52024504@newssvr13.news.prodigy.com> User-Agent: Pan/0.11.2 (Unix) Mime-Version: 1.0 Content-Type: text/plain; charset=iso885915 Content-Transfer-Encoding: 8bit X-Comment-To: "tmoran" NNTP-Posting-Host: 213.7.192.88 X-Trace: 1026816800 news.freenet.de 32670 213.7.192.88 X-Complaints-To: abuse@freenet.de Xref: archiver1.google.com comp.lang.ada:27142 Date: 2002-07-16T12:54:40-01:00 List-Id: On Mon, 15 Jul 2002 21:22:51 -0100, tmoran wrote: > Yes, you could do either of those assignment statements and yes you > would have a "memory leak" where the tail of the list would still be > occupying memory but would be inaccessible. OTOH, Then I don't undertand why the tutorial doesn't mention it. It says something like: "it's better to let the operating system manage it", but I haven't ever heard of an operating system which can manage memory (de)allocations (maybe Linux does it, but I have very little knowledge of how Linux works). This "drastic" technique is also shown in a book about programming I own (as an Ada example program, and *even* as a Pascal version, without "Dispose" !). I have also read that access types in Ada are more secure than pointers in other languages, but I don't see any reason, both versions seem to be identical. > This_List := head; > ... various uses of This_List > This_List := null; > would be OK since "head" would still be around to access the list, and > would probably be a good idea since it would prevent someone fiddling > with your list via This_List. For instance if you have changed to > head->0->1->... you probably don't want someone going directly to "1" > via a left over This_List. Let me know if I did understand this correctly: if I have a "head", which can access nodes (in a list), and I create some nodes and connect them,for instance: head -> 1 -> 2 -> 3 -> null and if I do nothing (so the program will end), the operating system (or compiler???) does the deallocation automatically, just because "head" is a variable. It then goes, starting at head, through the whole list and frees the memory occupied by every node. Right? If yes, why should I use a second head, which points at the main head? And how can I then access the nodes? With The_List.Head.all (first node) etc.? > What you need to do is a series of unchecked deallocations, say > > procedure Free is new Ada.Unchecked_Deallocation(...); > > procedure Free_List(x : List_Pointer) is begin > if x.next /= null then Free_List(x.next);end if; Free(x); > end Free_List; > ... > Free_List(head); That's what I explained, it's neccessary to dispose every node in other to delete the list. This recursive procedure is interesting, but it maybe not good for large lists (because of memory usage). >> but it's said that it's not recommended to use it (because some other > Unchecked_Deallocation has that name because it's not checked. Thus > you can really screw up, so it should be used carefully. Some systems > have automatic garbage collection, but most Ada systems don't (problems What is an Ada system? You mean systems for which an Ada compiler is available? > with real-time timing surprises, for instance). You can get some > automatic deallocation going for you with careful placement of "type ... > is access ..." Is this the thing I have explained above? > and especially with Storage_Pools. But mostly, with What is a storage pool? > variable size array declarations and functions that can return variable > size things, you find much less need for access types in Ada. I use pointers only for stacks (linked lists), trees etc. > You can > usually do stack, rather than heap, allocation which buys automatic, > safe, deallocation, and of course speed. What do you mean? Why should a FIFO be faster than a LIFO? Or do you mean something else by this. Thanks, Jan