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 = realloc (myarray, sizeof (char) * myarray_count);
myarray[myarray_count - 1] = 'H';
/* Do this a couple of more times... */

/* Now lets remove a character. */
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);
/* ... */

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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: