Return-Path: Delivered-To: apmail-incubator-stdcxx-dev-archive@www.apache.org Received: (qmail 11380 invoked from network); 23 May 2007 18:07:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 May 2007 18:07:48 -0000 Received: (qmail 52247 invoked by uid 500); 23 May 2007 18:07:53 -0000 Delivered-To: apmail-incubator-stdcxx-dev-archive@incubator.apache.org Received: (qmail 52193 invoked by uid 500); 23 May 2007 18:07:53 -0000 Mailing-List: contact stdcxx-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-dev@incubator.apache.org Received: (qmail 52182 invoked by uid 99); 23 May 2007 18:07:53 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 May 2007 11:07:53 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: local policy) Received: from [208.30.140.160] (HELO moroha.quovadx.com) (208.30.140.160) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 May 2007 11:07:46 -0700 Received: from qxvcexch01.ad.quovadx.com ([192.168.170.59]) by moroha.quovadx.com (8.13.6/8.13.6) with ESMTP id l4NI7Nk1014840 for ; Wed, 23 May 2007 18:07:23 GMT Received: from [10.70.3.113] ([10.70.3.113]) by qxvcexch01.ad.quovadx.com with Microsoft SMTPSVC(6.0.3790.1830); Wed, 23 May 2007 12:06:27 -0600 Message-ID: <4654832B.20806@roguewave.com> Date: Wed, 23 May 2007 12:08:43 -0600 From: Martin Sebor Organization: Rogue Wave Software User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.2) Gecko/20070221 SeaMonkey/1.1.1 MIME-Version: 1.0 To: stdcxx-dev@incubator.apache.org Subject: Re: std::vector intialization on HP-UX References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 23 May 2007 18:06:27.0697 (UTC) FILETIME=[153DAE10:01C79D65] X-Virus-Checked: Checked by ClamAV on apache.org Patrick Happel wrote: > Hello all, > I'm working with versions 4.1.2 and 4.1.0 of the STDCXX on HP-UX 11i > with aCC 3.63, and I've noticed that when a std::vector is constructed > with a size parameter, it's actually allocated as that size + 32 > additional elements. For example: > > #include > #include > > int main() { > std::vector testvec1(4); > std::cout << "Capacity: " << testvec1.capacity() << std::endl; > std::cout << "Size: " << testvec1.size() << std::endl; > return 0; > } > > When run will output: > > [phappel@moe 15d]$ ./stdvectest > Capacity: 36 > Size: 4 > > The HP-UX Standard Library will allocate 32 element sizes to a vector > declared with a size less than 32, but will allocate as expected beyond > that. STDCXX, however, seems like it will always add 32 to the size. > I'm curious if there is an explanation for this - is it perhaps some > quirk of memory allocation or template instantiation on HP-UX? Any > insight would be appreciated! Vector in stdcxx computes the capacity, C, from the desired size N, the Golden Ratio R (1.618), and the minimum capacity Cmin (32), using the following formula: C := max (N * R, N + Cmin) This is based on the assumption that more vectors will grow than not, and that by how much they will grow is a function of their current size. The Golden Ratio is known to strike an optimum balance between the amount of utilized space and the amount unused space. See, for example: http://en.wikipedia.org/wiki/Golden_ratio The HP implementation of vector (Rogue Wave libstd 2.2.1) seems to use a simpler formula: C := max (32, N) The classic implementation of vector shipped by HP (Rogue Wave libstd 1.2.1) as well as other popular implementations such as GNU libstdc++ or IBM XLC++ simply use N: C := N Martin