Dynar

Dynar is a dynamic array library written in C for C programs. In C++, the comparable data structure is called a vector and is included in the standard library. C doesn't have any similar functionality by default, so this is one possible solution. Note that C99's variable length arrays (VLAs) are not the same as a dynamic array. For VLAs, the size is determined at runtime but there is still no easy and standard means of resizing, copying, appending, and so forth.

With Dynar, you have those features and more. Fundamentally, Dynar is a series of functions built around a simple, common data structure. It tracks and manages memory allocations behind a central interface so that excessive and irregular memory manipulation code can be replaced.

Purpose

Most non-trivial C programs need to acquire and work on chunks of arbitrarily sized memory in order to be both flexible and efficient. Dynar provides that capability as a static or dynamic library.

During the design phase, the key purpose behind Dynar was to establish a data structure which would automatically maintain the size of an array for the user. This expanded to include functions beyond that, but it is still a fundamental property. This length management is critically important to avoiding serious bugs, including buffer overruns and multiple free calls, in any C program. Solving it in just one place helps preserve developer sanity and ensure a better design all at once.

Note that many, perhaps most, of the programming languages that came after C use a more tightly structured memory model. In Pascal and Ada, for example, an array and its length are instrinsically linked and can't be separated. Some languages, including Java and C#, have an entire memory management subsystem behind the scenes.

Documentation

Currently, the only API documentation is the inline comments of the code, which are rather thorough. The leading comment above each function in core.c should give you a very good idea of how the interface works. An overview of the interface can be found in api.h, the key header to be included by client programs. Code in the test suite might also prove enlightening.

Ultimately, separate documents with examples and abstracted explanations need to be written.

Licenses

This code is currently released under these licenses:

Choose one and only one for your usage.

I am considering an additional license, most probably either BSD or LGPL, but have not yet made a decision on that.

Releases

The most recent public version is 0.7.0, released on April 20th, 2012. Provided here is a bzip2 compressed tarball of the code.

Downloads: