220 13923 <m1mfbo$gli$1@ger.gmane.org> article
Path: news.gmane.org!not-for-mail
From: Bo Pesson <bop@gmb.dk>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Shrink dynamic arrays with delete[] operator
Date: Wed, 15 Oct 2014 20:45:10 +0200
Lines: 70
Approved: news@gmane.org
Message-ID: <m1mfbo$gli$1@ger.gmane.org>
References: <167019e1-7373-4688-b7ef-8da1a80acf07@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
X-Trace: ger.gmane.org 1413398735 17425 80.91.229.3 (15 Oct 2014 18:45:35 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 15 Oct 2014 18:45:35 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCZ6RLW3UIKBBR4B7OQQKGQEVOYVKRY@isocpp.org Wed Oct 15 20:45:31 2014
Return-path: <std-proposals+bncBCZ6RLW3UIKBBR4B7OQQKGQEVOYVKRY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wi0-f198.google.com ([209.85.212.198])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCZ6RLW3UIKBBR4B7OQQKGQEVOYVKRY@isocpp.org>)
	id 1XeTZh-0001Jb-UG
	for gclcip-std-proposals@m.gmane.org; Wed, 15 Oct 2014 20:45:29 +0200
Original-Received: by mail-wi0-f198.google.com with SMTP id hi2sf1184117wib.9
        for <gclcip-std-proposals@m.gmane.org>; Wed, 15 Oct 2014 11:45:29 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:to:from:subject:date:lines:message-id:references
         :mime-version:user-agent:in-reply-to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=MFCfhl07pCSnLCJsT4FvH5GtfwH791kdGNKBeGc2LYU=;
        b=IpD+CEx03u8/7MrD9NSERcoObnw8zp5URZAtVRXXU/TzPMBaDwuwyipMJJjDWevozd
         rK5rWTa9DNHw1R+u+E5uPIEsvUGFOP0Wc41bqS5jm0y+pKLl2hJ8rfuphXx7OC1hfx3K
         DYBksnctItkgzgejI08egprZE4PpQWI26lA3fjTQysDTKiuO4Cra/KDdjF8KfHWiIlru
         G3787oAKCYyXinNflWGwLalI0SYyC1bTKL8WZFCNxDec2S2dyzBYyuWiz9pJuisqK0p2
         Tf/o9yKW1u/Ywjif7AGorMfbopop5vLM7pO7Grg++BpbVSQ7ULeW4zeTOjPLIa9lM8AI
         iIXQ==
X-Gm-Message-State: ALoCoQlAXDCJ8AlGfdeynBwiyYHrn+0SDDepmhEjMysfCt8e+xc2KGvGVTckmia3ZHYzAiSlfnOJ
X-Received: by 10.180.93.35 with SMTP id cr3mr2417107wib.2.1413398729584;
        Wed, 15 Oct 2014 11:45:29 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.152.204.107 with SMTP id kx11ls91033lac.80.gmail; Wed, 15 Oct
 2014 11:45:27 -0700 (PDT)
X-Received: by 10.152.234.199 with SMTP id ug7mr14484116lac.26.1413398727627;
        Wed, 15 Oct 2014 11:45:27 -0700 (PDT)
Original-Received: from plane.gmane.org (plane.gmane.org. [80.91.229.3])
        by mx.google.com with ESMTPS id z7si31809675lag.18.2014.10.15.11.45.27
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=RC4-SHA bits=128/128);
        Wed, 15 Oct 2014 11:45:27 -0700 (PDT)
Received-SPF: pass (google.com: domain of gclcip-std-proposals@m.gmane.org designates 80.91.229.3 as permitted sender) client-ip=80.91.229.3;
Original-Received: from list by plane.gmane.org with local (Exim 4.69)
	(envelope-from <gclcip-std-proposals@m.gmane.org>)
	id 1XeTZa-0001EQ-Ut
	for std-proposals@isocpp.org; Wed, 15 Oct 2014 20:45:22 +0200
Original-Received: from c193-14-6-157.cust.tele2.se ([193.14.6.157])
        by main.gmane.org with esmtp (Gmexim 0.1 (Debian))
        id 1AlnuQ-0007hv-00
        for <std-proposals@isocpp.org>; Wed, 15 Oct 2014 20:45:22 +0200
Original-Received: from bop by c193-14-6-157.cust.tele2.se with local (Gmexim 0.1 (Debian))
        id 1AlnuQ-0007hv-00
        for <std-proposals@isocpp.org>; Wed, 15 Oct 2014 20:45:22 +0200
X-Injected-Via-Gmane: http://gmane.org/
Original-Lines: 61
Original-X-Complaints-To: usenet@ger.gmane.org
X-Gmane-NNTP-Posting-Host: c193-14-6-157.cust.tele2.se
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
In-Reply-To: <167019e1-7373-4688-b7ef-8da1a80acf07@isocpp.org>
X-Original-Sender: gclcip-std-proposals@m.gmane.org
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of gclcip-std-proposals@m.gmane.org designates 80.91.229.3 as
 permitted sender) smtp.mail=gclcip-std-proposals@m.gmane.org
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:13923
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/13923>

On 2014-10-15 19:29, contact@ncomputers.org wrote:
>
>   Shrink dynamic arrays on C++
>
>
> It is very useful to shrink arrays, which were created using the *new*
> operator.
>
> *For example:*
>
> Your code requires a billion of objects of a class to find a solution to
> a hard problem. As the code finds a new and better solution, some
> objects will become useless. Due to complexity, the code starts to
> require more time to process the useful data and find better solutions.
>
> As the code had a billion of useful objects in memory, it required only
> a few seconds to find a solution to the problem, because they were
> easier to find. When the code searches better solutions, it requires
> less objects and more time. For example, when it searches a solution
> with only one million of objects, it will delay a week to find a new and
> better solution to the problem.
>
> It would be a waste of memory to have 999 millions of useless objects
> for a whole week. Just imagine, if every object requires ten bytes, the
> code will be wasting 9.99 billions of bytes for a whole week.
>
> Thanks to the community of stackoverflow.com
> <http://www.stackoverflow.com>, cplusplus.com <http://www.cplusplus.com>
> and its contributors, we considered two possible solutions to shrink
> arrays:
>
>   * Allocate memory using the *std::malloc* function and shrink the
>     array using the *std::realloc* function.
>   * Create a *new* array, copy the useful objects and *delete* the old
>     arrays.
>
> Both are good solutions.
>
> But we desire a solution, which is oriented to objects, automatically
> calls the destructors, is a C++ standard compatible with most memory
> allocators and neither moves the useful data nor copies it, due to
> performance.
>
>   * To use the *std::malloc* and *std::realloc* functions is a solution,
>     which requires to overload the *new* and *delete* operators to make
>     it oriented to objects.
>   * To create a *new* array, copy the useful objects and *delete* the
>     old array is a solution, which would be very slow if we shrink the
>     array a billion of times.
>   * The performance offered by the *std::vector* when shrinking could be
>     overcome by other implementations or native arrays
>     <http://ncomputers.org/content/code.php?src=examples/shrink%20arrays%201.cpp>.
>

I believe your calls to realloc(p, size-1) actually doesn't free up any 
memory, which of course would be faster than shrinking the vector.


Bo Persson



-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.
