.\" Part of publib. .\" .\" Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" .\" 2. Redistributions in binary form must reproduce the above .\" copyright notice, this list of conditions and the following .\" disclaimer in the documentation and/or other materials provided .\" with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS .\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING .\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" .\" part of publib .\" "@(#)publib-alloc:$Id: dynarr.3,v 1.2 1995/07/31 19:36:20 liw Exp $" .\" .TH DYNARR 3pub "C Programmer's Manual" "Publib" "C Programmer's Manual" .SH NAME dynarr, dynarr_init, dynarr_resize, dynarr_free \- simple dynamic arrays .SH SYNOPSIS #include .sp 1 .nf void \fBdynarr_init\fR(struct dynarr *\fIda\fR, size_t \fIelsize\fR); int \fBdynarr_resize\fR(struct dynarr *\fIda\fR, size_t \fInewsize\fR); void \fBdynarr_free\fR(struct dynarr *\fIda\fR); .SH "DESCRIPTION" These functions make it easier to use dynamic arrays, i.e., arrays that are allocated with \fImalloc\fR(3) and resized with \fIrealloc\fR(3). Below is a typical code fragment for implementing a dynamic array that is resized as more input is read. .in +5 .sp 1 .nf char *p, *line; size_t alloc, len; len = 0; alloc = 1024; if ((line = malloc(alloc)) == NULL) abort(); while (fgets(line + len, alloc-len, stdin) != NULL) { len = strlen(line); alloc += 1024; if ((p = realloc(alloc)) == NULL) abort(); alloc = p; } .fi .in -5 .sp 1 (The error handling is intentionally simplified.) Below is the above fragment with the \fIdynarr\fR(3). .in +5 .sp 1 .nf struct dynarr da; dynarr_init(&da); while (fgets((char *)da.data + da.used, da.alloc-da.len, stdin) != NULL) { da.used = strlen(da.data); if (dynarr_resize(&da, da.alloc + 1024) == -1) abort(); } .fi .in -5 .sp 1 The code is a bit simpler, and all the memory allocation details and most of the error checking code is hidden away. .PP The dynamic array is represented by a \fIstruct dynarr\fR: .in +5 .sp 1 .nf struct dynarr { void *data; size_t alloc, used; }; .fi .in -5 .sp 1 The interface to the dynamic allocation has intentionally been made unopaque. .PP \fIdynarr_init\fR initializes a struct dynarr to be an empty array, \fIdynarr_resize\fR sets its size to be \fInewsize\fR, and \fIdynarr_free\fR frees the array (it will become an empty array again). .SH RETURNS \fIdynarr_resize\fR returns -1 if it failed, 0 if it succeeded. It does not change the array in any way if it failed. .SH "SEE ALSO" publib(3), malloc(3), realloc(3), strdup(3) .SH AUTHOR Lars Wirzenius (lars.wirzenius@helsinki.fi)