comp.lang.ada
 help / color / mirror / Atom feed
From: "David Thompson" <david.thompson1@worldnet.att.net>
Subject: Re: access / freeing memory
Date: Wed, 24 Jul 2002 00:25:24 GMT
Date: 2002-07-24T00:25:24+00:00	[thread overview]
Message-ID: <Utm%8.833$pg2.58667@bgtnsc05-news.ops.worldnet.att.net> (raw)
In-Reply-To: 3D343EF9.3051A6C2@san.rr.com

A slight clarification:

Darren New <dnew@san.rr.com> 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






  parent reply	other threads:[~2002-07-24  0:25 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-16  0:52 access / freeing memory Jan Prazak
2002-07-15 22:22 ` tmoran
2002-07-16 13:54   ` Jan Prazak
2002-07-16 11:51     ` Fabien Garcia
2002-07-16 22:59       ` Jan Prazak
2002-07-16 15:42     ` Darren New
2002-07-16 22:59       ` Jan Prazak
2002-07-17  5:22         ` Simon Wright
2002-07-17 21:36           ` Jan Prazak
2002-07-24  0:25       ` David Thompson [this message]
2002-07-23  6:15   ` Kevin Cline
2002-07-18 18:22 ` chris.danx
2002-07-19 13:32   ` Jan Prazak
2002-07-19 23:50     ` chris.danx
     [not found] <mailman.1026804243.16337.comp.lang.ada@ada.eu.org>
2002-07-19  1:42 ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox