N-Dimensional Array Allocation in C


Posted: 31 May 2013

Tagged: C Snippet

As languages go, C is not well known for its support of multi-dimensional arrays. While recent standards updates in the C99 and C11 releases have increased the support for allocation and use of both statically and dynamically allocated arrays using the type name[size1][size2]...[sizen] syntax, it retains significant limitations. The biggest of which is that it allocates the array entirely on the stack, limiting data scope and - in my experience - often leading to overflows.

When working with scientific codes, especially those which were originally written in Fortran with its efficient native Multi-Dimensional Array support, it is very common to run into applications that use heap allocated multi-dimensional arrays allocated more or less like this.

void *alloc2d(size_t item_size, size_t len1, size_t len2){
void ** arr = (void**)malloc(len1*sizeof(void*));
for(int i=0; i<len1; i++){
arr[i] = malloc(len2*item_size);
}
return arr;
}
view raw alloc2dp.c hosted with ❤ by GitHub

The result works fine for the main purpose of generating such an array, accessibility by the arr[i][j] syntax, just as though it were allocated on the stack with the first method. Even so, this method has significant downsides. Now we have len2+1 arrays which are likely non-contiguous in memory, hurting caching behavior and increasing allocation time. As a result, I find myself frequently writing little allocators to allocate a single flat array with the necessary references in it. While each of these is simple, it bothers me to have multiple copies of functions like alloc2d(...) and alloc3d and so-forth where it seems a single function should do.

Working with a particularly nasty code this week that uses multi-dimensional arrays of varying size and dimension from one to seven this week convinced me to finally do something about it. I make no claim that the following is optimal nor that it is pretty or as safe as possible, but it is a general multi-dimensional allocator in C.

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
typedef struct {
uint64_t ind_size;
void **ind_start;
uint64_t arr_size;
void *arr_start;
void **current_ind;
int sentinel;
} dopev;
void md_malloc(dopev &d)
{
dopev *array = (dopev *)malloc(d.arr_size + d.ind_size + sizeof(dopev));//allocate
if(array == NULL) {
errno = ENOMEM;
perror("md_alloc failed, malloc returned NULL:");
exit(ENOMEM);
}
array[0] = d;
d.ind_start = (void **)(array + 1);
d.current_ind = d.ind_start;
d.arr_start = (void *)(((char *)d.ind_start) + d.ind_size);
//fprintf(stderr, "allocated %p\n", array);
}
void *md_map(int dims, uint64_t item_size, const uint64_t counts[], dopev &d, char *arr, void **ind)
{
if(dims == 1) {
return arr;
} else {
uint64_t dim_prod = item_size;
for(int i = 1; i < dims; ++i) {
dim_prod *= counts[i];
}
void **outer = d.current_ind;
d.current_ind += counts[0];
//fprintf(stderr, "ind taking %llu, dims=%d ind=%p, ind_start=%p, arr=%p, arr_start=%p\n", (uint64_t)sizeof(void *) * counts[0], dims, ind, d.ind_start, arr, d.arr_start);
for(int i = 0; i < counts[0]; ++i) {
outer[i] = md_map(dims - 1, item_size, counts + 1, d, arr + (dim_prod * i), d.current_ind);
}
return outer;
}
}
uint64_t ind_size(int dims, const uint64_t counts[])
{
if(dims > 1) {
uint64_t size = counts[0];
size += counts[0] * ind_size(dims - 1, counts + 1);
return size;
} else {
return 0;
}
}
void *md_alloc(int dims, uint64_t item_size, const uint64_t counts[], int initialize, int val)
{
if(counts == NULL) {
errno = EINVAL;
perror("md_alloc failed, a NULL sizes list is not allowd:");
exit(EINVAL);
}
dopev dope = {.sentinel = 0xDEADBEEF};
dope.arr_size = item_size;
for(int i = 0; i < dims; ++i) { //calculate complete size
dope.arr_size *= counts[i];
}
dope.ind_size = sizeof(void *)*ind_size(dims, counts); //accounts for 1d arrays
md_malloc(dope);
if(initialize) {
memset(dope.arr_start, val, dope.arr_size);
}
void *arr = md_map(dims, item_size, counts, dope, (char *)dope.arr_start, dope.ind_start);
//fprintf(stderr, "ind_size=%llu, ind used=%llu\n", dope.ind_size, (uint64_t)dope.current_ind - (uint64_t)dope.ind_start);
return arr;
}
void md_free(void *ptr)
{
dopev *arr = ((dopev *)ptr) - 1;
if(ptr == NULL || arr[0].sentinel != 0xDEADBEEF) {
errno = EINVAL;
perror("md_free failed, a NULL pointer or a non-md array specified:");
throw;
exit(EINVAL);
}
free(arr);
}
view raw allocndp.c hosted with ❤ by GitHub

The md_alloc() and md_free() functions have been tested to correctly manage arrays of up to 12 dimensions with a single allocation to store both the data and all associated pointer state. It may be complex, but no more duplicated allocator functions, finally! Now if only there were a way to get rid of the need for all the pointers in C as one does in c++ with the likes of boost::multi_array or blitz++. Maybe someday.