From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29700 invoked by alias); 5 May 2017 11:48:42 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 24318 invoked by uid 89); 5 May 2017 11:48:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=no version=3.3.2 spammy=wish X-HELO: mx1.redhat.com DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E462F4E338 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=fweimer@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com E462F4E338 Subject: Re: [PATCH] Dynamic growable arrays for internal use To: Joseph Myers Cc: GNU C Library References: From: Florian Weimer Message-ID: <373c24b0-4d73-cf0a-b264-8a958aff6f2b@redhat.com> Date: Fri, 05 May 2017 11:48:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-SW-Source: 2017-05/txt/msg00104.txt.bz2 On 04/24/2017 04:06 PM, Joseph Myers wrote: > On Sat, 22 Apr 2017, Florian Weimer wrote: > >> +/* To use the dynarray facility, you need to include >> + and define the parameter macros >> + documented in that file. >> + >> + A minimal example which provides a growing list of integers can be >> + defined like this: >> + >> + struct int_array >> + { >> + int *array; >> + size_t length; >> + }; >> + >> + #define DYNARRAY_STRUCT dynarray_int >> + #define DYNARRAY_ELEMENT int >> + #define DYNARRAY_PREFIX dynarray_int_ >> + #define DYNARRAY_FINAL_TYPE struct int_array >> + #include >> + >> + To create a three-element array with elements 1, 2, 3, use this >> + code: >> + >> + struct dynarray_int dyn; >> + dynarray_int_init (&dyn); >> + int *place = dynarray_int_emplace (&dyn); > > Should this first call to dynarray_int_emplace outside the loop be there? > The result doesn't seem to be used. Right, I didn't test the example at all. There are many errors in it. The corrected version follows. A minimal example which provides a growing list of integers can be defined like this: struct int_array { int *array; size_t length; }; #define DYNARRAY_STRUCT dynarray_int #define DYNARRAY_ELEMENT int #define DYNARRAY_PREFIX dynarray_int_ #define DYNARRAY_FINAL_TYPE struct int_array #include To create a three-element array with elements 1, 2, 3, use this code: struct dynarray_int dyn; dynarray_int_init (&dyn); for (int i = 1; i <= 3; ++i) { int *place = dynarray_int_emplace (&dyn); assert (place != NULL); *place = i; } struct int_array result; bool ok = dynarray_int_finalize (&dyn, &result); assert (ok); assert (result.length == 3); assert (result.array[0] == 1); assert (result.array[1] == 2); assert (result.array[2] == 3); free (result.array); I wish we had doctests for these kinds of things. Does anyone have comments about the interface and implementation? Thanks, Florian