Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

dynarr_init(3pub) [debian man page]

DYNARR(3pub)						       C Programmer's Manual						      DYNARR(3pub)

NAME
dynarr, dynarr_init, dynarr_resize, dynarr_free - simple dynamic arrays SYNOPSIS
#include <publib.h> void dynarr_init(struct dynarr *da, size_t elsize); int dynarr_resize(struct dynarr *da, size_t newsize); void dynarr_free(struct dynarr *da); DESCRIPTION
These functions make it easier to use dynamic arrays, i.e., arrays that are allocated with malloc(3) and resized with realloc(3). Below is a typical code fragment for implementing a dynamic array that is resized as more input is read. 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; } (The error handling is intentionally simplified.) Below is the above fragment with the dynarr(3). 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(); } The code is a bit simpler, and all the memory allocation details and most of the error checking code is hidden away. The dynamic array is represented by a struct dynarr: struct dynarr { void *data; size_t alloc, used; }; The interface to the dynamic allocation has intentionally been made unopaque. dynarr_init initializes a struct dynarr to be an empty array, dynarr_resize sets its size to be newsize, and dynarr_free frees the array (it will become an empty array again). RETURNS
dynarr_resize returns -1 if it failed, 0 if it succeeded. It does not change the array in any way if it failed. SEE ALSO
publib(3), malloc(3), realloc(3), strdup(3) AUTHOR
Lars Wirzenius (lars.wirzenius@helsinki.fi) Publib C Programmer's Manual DYNARR(3pub)

Check Out this Related Man Page

XMALLOC(3pub)						       C Programmer's Manual						     XMALLOC(3pub)

NAME
xmalloc, xrealloc, xfree, xstrdup, xmemdup, memdup - memory allocation functions for Publib SYNOPSIS
#include <publib.h> void *xmalloc(size_t bytes); void *xrealloc(void *ptr, size_t bytes); void xfree(void *ptr); char *xstrdup(const char *string); void *memdup(const void *mem, size_t bytes); void *xmemdup(const void *mem, size_t bytes); DESCRIPTION
These functions are utility functions for memory allocation from the publib library. xmalloc, xrealloc, and xfree are error checking ver- sions of the standard library routines malloc, realloc, and free, respectively. They are guaranteed to never return unless there was no problem: if, for example, xmalloc is unable to allocate the requested amount of memory, it prints an error message and terminates the pro- gram. Hence, the caller does not need to check for a NULL return value, and the code that calls these functions is simpler due to the lack of error checks. Similarly, xstrdup is an error checking version of the common (though not standard) strdup routine, which creates a duplicate of a string by allocating memory for the copy with malloc. (For systems that lack strdup, publib provides one in its portability module; it is always declared in <publib.h>.) memdup is similar to strdup, it creates a copy of an arbitrary memory area (the arguments are a pointer to the beginning of the area, and its size) by allocating memory for the copy with malloc. xmemdup is its error checking version. NOTE
xmalloc and xrealloc treat a request to allocate a block of 0 bytes as an error. xrealloc will allow its first argument to be NULL. SEE ALSO
publib(3), malloc(3), strdup(3) AUTHOR
Lars Wirzenius (lars.wirzenius@helsinki.fi) Publib C Programmer's Manual XMALLOC(3pub)
Man Page