From 5726902157928954865 X-Google-Thread: f78e5,27c2a088314ec0a9 X-Google-Attributes: gidf78e5,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!news2.telebyte.nl!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull From: dhruvbird@gmx.net ("Dhruv Matani") Newsgroups: comp.std.c++ Subject: Re: should std::vector<> exponential growth rate be followed strictly in times of low availabe memory. Date: Sun, 7 Nov 2004 17:30:55 GMT Lines: 51 Sender: mail2news@demon.net Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++) Message-ID: References: <417E434E.5000209@bubblescope.net> <4fvjo0t07isfber4rblifh56jmiq2glrnj@4ax.com> <4fb4137d.0411050721.1934c7c0@posting.google.com> NNTP-Posting-Host: news.news.demon.net Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: news.demon.co.uk 1099848663 5739 158.152.254.254 (7 Nov 2004 17:31:03 GMT) X-Complaints-To: abuse@demon.net NNTP-Posting-Date: Sun, 7 Nov 2004 17:31:03 +0000 (UTC) X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov) X-User-Agent: Pan/0.13.3 (That cat's something I can't explain) X-Orig-X-Trace: news.uni-berlin.de Qm021mmslo9A/+J81dJwgQlnHGoeCldsrxeV9oYLiL1HP23Cbz X-Virus-Scanned: by amavisd-new at cs.mu.OZ.AU X-Received: (from fjh@localhost) by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id iA7HUtSp011359; Mon, 8 Nov 2004 04:30:55 +1100 (EST) X-Path: comp-std-cpp-robomod!not-for-mail X-Delivered-To: std-c++@ucar.edu X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f X-Newsgroups: comp.std.c++ Xref: g2news1.google.com comp.std.c++:3302 On Sat, 06 Nov 2004 18:39:21 +0000, Dhruv Matani wrote: [snip]... >> Of course, it's not hard to implement your "reallocation fallback" >> policy with a wrapper function like this: >> >> template < class T, class A > >> void modified_push_back ( std::vector& v, const T& t ) >> { >> try { >> v.push_back(t); >> } >> catch (std::bad_alloc&) { >> std::vector v2; _________[0] >> v2.reserve(v.capacity() + 1 ); // or whatever >> std::copy( v.begin(), v.end(), std::back_inserter(v2) ); >> v.swap(v2); >> } > > I don't think this would work for the simple reason that a vector > implementation is not required to allocate ONLY n bytes(memory for n > objects) after a reserve(n) > command. The guarantee is that at least n objects can be inserted(assuming > that it is empty initially) without reallocation. The vector can do this > internally: > > vector::reserve(int n) > { > int new_size = std::max(this->capacity()*2, n); > // Normal stuff. > } > > And we would be back to square one. Ah! Sorry, I did not see [0]. I was under the impression that you are reserving space in the original vector. Yes, this would work! Regards, -Dhruv. --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]