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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c32f6f0b23106020 X-Google-Attributes: gid103376,public From: tmoran@bix.com Subject: Re: Large strings in ADA Date: 2000/04/17 Message-ID: #1/1 X-Deja-AN: 612376085 References: <38FB4521.D02EA4C9@xpress.se> X-Complaints-To: abuse@pacbell.net X-Trace: news.pacbell.net 956002271 206.170.24.103 (Mon, 17 Apr 2000 13:11:11 PDT) Organization: SBC Internet Services NNTP-Posting-Date: Mon, 17 Apr 2000 13:11:11 PDT Newsgroups: comp.lang.ada Date: 2000-04-17T00:00:00+00:00 List-Id: > > 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.