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 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-23 17:25:28 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.mathworks.com!wn3feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!not-for-mail From: "David Thompson" Newsgroups: comp.lang.ada References: <%WHY8.1173$tt5.52024504@newssvr13.news.prodigy.com> <3D343EF9.3051A6C2@san.rr.com> Subject: Re: access / freeing memory X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: Date: Wed, 24 Jul 2002 00:25:24 GMT NNTP-Posting-Host: 12.89.145.93 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1027470324 12.89.145.93 (Wed, 24 Jul 2002 00:25:24 GMT) NNTP-Posting-Date: Wed, 24 Jul 2002 00:25:24 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:27345 Date: 2002-07-24T00:25:24+00:00 List-Id: A slight clarification: Darren New wrote : (other good stuff about memory allocation/management snipped) ... > > > You can > > > usually do stack, rather than heap, allocation which buys automatic, > > > safe, deallocation, and of course speed. ... > He means usually you can allocate a local variable with a > declare...begin...end (which allocates information on the CPU's stack) > rather than using "new" to allocate something and then specifically have to > free it up. He's basically comparing how you do variable-sized objects in > Ada to how you do variable-sized objects in C (which has no variable-sized > objects, so you have to use "new" (aka malloc) to allocate them) or Java > (which has all objects allocated from the heap, so can't put them on the > stack for efficiency). > In C you have malloc (and calloc and realloc) which only allocates raw memory (and for calloc initializes it to all zero bits, which is often but not required to be a useful value such as a zero integer, null pointer, etc.), and free which deallocates it. Some implementations also have alloca(), which allocates variable-sized space on the stack in the current function's (subprogram's) frame, but this is nonstandard. In C++ you can still use the C forms, but also have 'new' which allocates memory _and runs a (possibly trivial) constructor for the new object's type_ and 'delete' which _runs the destructor and_ frees the underlying memory. (Actually you also have placement new which _only_ runs the constructor, using memory you have allocated by other means, but that's just confusing the issue.) The memory allocation and deallocation part of 'new' and 'delete' typically is actually done by malloc and free, but this is not required. And in C99 (and perhaps in some future C++), AFAIK not yet implemented outside gcc (where it was already an extension), you can have local (stack) _arrays_ with runtime/nonstatic sizes, called logically enough Variable Length Arrays. But not similarly for the other things Ada discriminated and/or private types can do. In the Java spec it is certainly true that all objects (of nonprimitive types) are on the heap. However, for an object that is actually used only locally within one function, it is my understanding that a decent "real" (native) compiler can actually put it on the stack, which the resulting efficiency gains. I'm not sure if it's practical for a JIT to do the same, but it's presumably at least permitted. -- - David.Thompson 1 now at worldnet.att.net