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=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!attcan!uunet!samsung!usc!zaphod.mps.ohio-state.edu!mips!gumby!murphy From: murphy@mips.COM (Mike Murphy) Newsgroups: comp.lang.ada Subject: Re: C Strings in Ada? Message-ID: <39309@mips.mips.COM> Date: 11 Jun 90 22:30:52 GMT References: <920024@hpclapd.HP.COM> <920025@hpclapd.HP.COM> <1304@software.software.org> <57245@bbn.BBN.COM> <8446@crdgw1.crd.ge.com> <57288@bbn.BBN.COM> Sender: news@mips.COM Reply-To: murphy@mips.COM (Mike Murphy) Organization: MIPS Computer Systems, Sunnyvale, CA List-Id: In article <57288@bbn.BBN.COM> adoyle@vax.bbn.com (Allan Doyle) writes: >When I am using the Ada "new" keyword, I know full well that I'm creating >potential for garbage. I can control when I call it. I can write my own >memory manager around it. I don't get that control when I need a construct >like > > declare > c_string : constant string := "foo" & ascii.nul; > begin > > end; > >If I can't even trust Ada to reclaim the 4 or so bytes used by foo, what >can I trust Ada to do???? Most Ada compilers will reclaim the space used by an object when it exits its scope. I say "most" because this is not required by the semantics of the language, but any good compiler will have this feature, as it is needed for large software systems, and it is not too difficult to implement. Basically, you emit some code at the end of any blocks or statements that use temporary space, to reclaim that space. This might mean a "free", or it could be something even cheaper (e.g. some compilers will put these kind of block-structured temporaries on a mark-release stack). You can code up a little example like the above to test what your compiler does. This is not considered to be "real" garbage-collection, which no Ada compiler I know of does, which requires searching through memory for dangling pointers. Instead this is just an important optimization for a common situation in Ada. The situation is more common in Ada than in languages like C because we can have function calls that return dynamic-sized objects, such as strings, and these need temporary space. The reclaiming of the space is helped by the block-structured nature of its scope (as opposed to the full-blown garbage collection problem). -- Mike Murphy -- UUCP: sun!decwrl!mips!murphy or murphy@mips.com