comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@bix.com
Subject: Re: Large strings in ADA
Date: 2000/04/17
Date: 2000-04-17T00:00:00+00:00	[thread overview]
Message-ID: <zdKK4.3$vE6.542@news.pacbell.net> (raw)
In-Reply-To: 38FB4521.D02EA4C9@xpress.se

> >      Msg : String(1 .. 2_500_000);  -- 2.5 MB
>
> The above is the current solution but it doesn't work as it uses to much
> memory.
  It seems to me 2.5MB is the smallest possible amount of memory
needed to hold 2.5MB of bytes.

> The C-program reallocates the buffer only if the string added to
> it exceeds its current size and if it reallocates it grows with a power
> of 2. So if the buffer was 8 bytes big and you added 9 bytes the buffer
> would become 32 bytes big.
  So when the buffer is 2,097,152 (2^21) bytes long, and you add
a string on your way to 2,500,000, the C program will for a moment
have a 2^21 old buffer allocated, plus a 2^22 new buffer, for a
total memory requirement of 6,291,456 bytes as the high point.  So
the C approach must be able to allocate 2.5 *times* as much memory
as the simple Ada declaration.  Why?  If it's because your system
allows a large heap, but only a small (less than 2.5MB) area for
a Msg : String(1 .. 2_500_000); declaration, then how about
  type Msgs is array(1 .. 2_500_000) of aliased Character;
  type Ptr_To_Msgs is access all Character;
  P_Msg : Ptr_To_Msgs := new Msgs;
and then use the buffer at P_Msg instead of Msg.
  Note that the "double the size and reallocate" technique requires
copying the old buffer each time.  It will copy nearly 2^22=4M of
old bytes, in addition to the 2.5Mb of new bytes.  Even on a fast
machine, that's a chunk.




  reply	other threads:[~2000-04-17  0:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-16  0:00 Large strings in ADA Johan Groth
2000-04-16  0:00 ` David Starner
2000-04-17  0:00 ` Robert Dewar
2000-04-17  0:00 ` Florian Weimer
2000-04-17  0:00 ` Dale Stanbrough
2000-04-17  0:00   ` tmoran
2000-04-17  0:00     ` Johan Groth
2000-04-17  0:00       ` tmoran [this message]
2000-04-18  0:00         ` tmoran
2000-04-22  0:00           ` Johan Groth
2000-04-17  0:00       ` Florian Weimer
2000-04-17  0:00       ` Robert Dewar
2000-04-17  0:00         ` David Starner
2000-04-17  0:00 ` Robert Dewar
replies disabled

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