comp.lang.ada
 help / color / mirror / Atom feed
From: dennison@telepath.com
Subject: Re: Unbounded array
Date: 1999/04/29
Date: 1999-04-29T00:00:00+00:00	[thread overview]
Message-ID: <7g9qn5$g73$1@nnrp1.dejanews.com> (raw)
In-Reply-To: m3g15krlw5.fsf@mheaney.ni.net

In article <m3g15krlw5.fsf@mheaney.ni.net>,
  Matthew Heaney <matthew_heaney@acm.org> wrote:
> Pawel Kobylarz <kobylarz@wbkst21.mach.uni-karlsruhe.de> writes:
>
> > Realloc function takes two parameters, the actual pointer and new
> > size, and it preserves the data already present in the memory block.
> > Realloc is more efficient than allocate-copy-deallocate, because it
> > can add a piece of memory without moving existing data if there is
> > place after the existing block.
>
> But that isn't guaranteed.  You save the cost of a copy only if there's
> an adjacent block large enough to contain the new part of the array.

I wouldn't be suprised if it didn't *always* do the copy on some platforms.
If if it does try to extend, unless the malloc algorithm is specificly
optimized to leave holes for realloc, most of the time its probably going to
have to copy anyway.

> > In my textbook, I found only operators new and unchecked deallocation,
> > nothing similar to realloc(). Is there in ADA something like
> > realloc?
>
> No, it is not built in.  However, you could
>
> 1) write a binding the C function

It should be noted that if you do this, you should also write bindings to
malloc or calloc and free, and you should use those calls rather than Ada's
"new" and Unchecked_Deallocation. "new" is not guaranteed to be a straight
call to malloc, so you could seriously hose things by trying to mix Ada and C
allocations and deallocations.

>
> 2) write your own storage pool that supports a realloc-style operation,
>    and bind your access type to that pool; see RM95 13.11

That's probably the *best* solution. But of course it is also the most work
and trickiest to implement correctly.

Of course there's a third possibility:

 3) Write a "Realloc (Ptr : in out ..." for your specific access type and do
the "new", copy, and "Unchecked_Deallocation" in its body. If all Pawel was
looking for was the convienence of doing that in one operation, this is
probably the best way.

--
T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




      reply	other threads:[~1999-04-29  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-04-26  0:00 Unbounded array Pawel Kobylarz
1999-04-26  0:00 ` Robert Dewar
1999-04-27  0:00   ` Pawel Kobylarz
1999-04-27  0:00   ` Pawel Kobylarz
1999-04-28  0:00     ` Matthew Heaney
1999-04-29  0:00     ` Matthew Heaney
1999-04-29  0:00       ` dennison [this message]
replies disabled

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