DynArray

DynArray is a C header containing X-Macros for the creation and manipulation of dynamically resizing arrays. It includes functions for adding elements, removing elements, and getting and setting array slots.

Most C programmers have to go through a crazy mess of code to get dynamically resizing code arrays.

/* Example of a dynamic array of characters. */
int myarray_count = 0;
char *myarray = NULL;

/* Add an element. */
myarray_count++;
myarray = realloc (myarray, sizeof (char) * myarray_count);
myarray[myarray_count - 1] = 'H';
/* Do this a couple of more times... */

/* Now lets remove a character. */
myarray_count--;
int i;
for (i = 5 /* because we want to remove the element at index 5 */; i < myarray_count - 1; i++)
myarray[i] = myarray[i + 1];
myarray = realloc (myarray, sizeof (char) * myarray_count);

Now that is a lot of work for what should be simple! With DynArray, however, it is easy as a few function calls.

DYNA_DECLARE (char, myarray);
DYNA_INIT (char, myarray);
DYNA_ADD ('H');
/* ... */
DYNA_REMOVE (5);

Beautiful, eh? Download it at https://savannah.nongnu.org/projects/dyna/.

Annoy other people with your interests!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>