Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"What I've done, of course, is total garbage." -- R. Willard, Pure Math 430a


devel / comp.lang.c / Re: My dynamic arrays module [was:Re: Pointer conversion and alignment]

SubjectAuthor
o Re: My dynamic arrays module [was:Re: Pointer conversion andAnton Shepelev

1
Re: My dynamic arrays module [was:Re: Pointer conversion and alignment]

<20220206172703.c3bcd39b5f7d49779713e2be@gmail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=20381&group=comp.lang.c#20381

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: My dynamic arrays module [was:Re: Pointer conversion and
alignment]
Date: Sun, 6 Feb 2022 17:27:03 +0300
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <20220206172703.c3bcd39b5f7d49779713e2be@gmail.com>
References: <20181117143819.43938ee34006bbcc1826bf0c@gmail.com>
<20181119022614.5cf93af72a29cb41cdc86910@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="736f2dfe15328b5073b344b8c3ae06b4";
logging-data="9969"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+HC9SbWVDkR6pNT6/IrFbSl9byFgrxr+o="
Cancel-Lock: sha1:394ALBZ4sdbrxgpcCuSXP1nQrhA=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Sun, 6 Feb 2022 14:27 UTC

Hello, all.

Some of you may remember my dynamic arrays for C:

Message-ID: <20181119022614.5cf93af72a29cb41cdc86910@gmail.com>

In short, they are dynamic arrays that work and look like
normal C arrays with respect to element access:

int * a;

/* Create a new array: */
a = da_new( sizeof( int ) );
if( a == NULL ) goto ERROR;

/* Set length 2: */
a = da_setlen( a, 2 );
if( a == NULL ) goto ERROR;

/* Access elements as with normal arrays: */
a[0] = 0; a[1] = 1;

/* Append an element: */
int v = 2;
a = da_add( a, &v );
if( a == NULL ) goto ERROR;

/* Print: */
int l = da_len( a );
for( int i = 0; i < l; i++ )
{ printf("%i ", a[i]); }

/* Dispose: */
da_free( a );

Observe that operations that may result in a memory
reallocation are implemented as funcitons returning the
(possibly) new value of the array. If such an operation
fails, it frees the array and returns NULL, so that that the
caller can detect a failure to allocate more memory but
cannot salvage the data.

I did not like this and the repetition of the array
identifer, and instead of writing:

a = da_setlen( a, newlen );
if( a == NULL ) goto ERROR;

I should prefer to write:

if( !da_setlen( &a, newlen) ) goto ERROR;

but it seems impossible in macro-less C, because void** is
incompatible with int**. What do you think of the following
alternative:

a = da_setlen( a, newlen );
if( !da_ok( a ) )
{ save_data( a ); /* whatever you need */
da_free ( a );
goto ERROR;
}

Now, da_setlen() reports failure by setting an internal flag
testable by da_ok() and returning the old array to avoid
data loss. Would this version be more useful from your
experieice?

--
() ascii ribbon campaign -- against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor