Generic dynamic array implementation in C I'm working on my own generic-type dynamic array in C99 and I would like to hear your input, suggestions, ideas, anything! 

What I most care about is: 1) correctness and 2) performance.

So far, I've tested both with standard malloc and mimalloc, although with the standard implementation it seems to be running 50% faster. 

I've also ran benchmarks against libsrt, vec and Nim's implementation of dynamic sequences and my code seems to be beating them all.

Also, of major importance is to note that this code is not meant to be used as a library, but as part of a bytecode VM/intepreter that I've been working on - which means I am the one who is going to use it.

Here's the source:

#ifndef __VARRAY_H__
#define __VARRAY_H__

/**************************************
  Type definitions
 **************************************/

#define Array(TYPE) \
    struct { \
        size_t size; \
        size_t cap; \
        size_t typeSize; \
        TYPE *data; \
    }

/**************************************
  Macros
 **************************************/

#define aInit(DEST,TYPE,CAP) { \
    int k=0;                                                                \
    while (powerOfTwo[++k]&lt;CAP);                                            \
                                                                            \
    DEST = malloc(sizeof(Array(TYPE)));                                     \
    DEST-&gt;size = 0;                                                         \
    DEST-&gt;typeSize = sizeof(TYPE);                                          \
    DEST-&gt;cap = powerOfTwo[k];                                              \
    DEST-&gt;data = calloc(DEST-&gt;cap, DEST-&gt;typeSize);                         \
}

#define aNew(NAME,TYPE,CAP) \
    Array(TYPE)* NAME;                                                      \
    aInit(NAME,TYPE,CAP);

#define aResize(DEST) { \
    DEST-&gt;cap *= 2;                                                         \
    DEST-&gt;data = realloc(DEST-&gt;data, DEST-&gt;cap * DEST-&gt;typeSize);           \
}

#define aAdd(DEST,X) \
    if (++(DEST-&gt;size) &gt;= DEST-&gt;cap) aResize(DEST);                         \
    DEST-&gt;data[DEST-&gt;size-1] = X

#define aAppend(DEST,X) \
    DEST-&gt;cap += X-&gt;cap;                                                    \
    DEST-&gt;data = realloc(DEST-&gt;data, DEST-&gt;cap * DEST-&gt;typeSize);           \
    memcpy(DEST-&gt;data + DEST-&gt;size, X-&gt;data, X-&gt;size * X-&gt;typeSize);        \
    DEST-&gt;size += X-&gt;size

#define aEach(DEST,INDEX) \
    for (int INDEX=0; INDEX&lt;DEST-&gt;size; INDEX++)

#define aFree(DEST) \
    free(DEST-&gt;data);                                                       \
    free(DEST)

#endif


The powerOfTwo lookup constant:

/**************************************
  Constants
 **************************************/

static size_t powerOfTwo[] = {
    0,
    1, 2, 4, 8, 16, 32, 64, 128,
    256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
    65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608,
    16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648,
    4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888
};


And a little usage example:

Array(int)* arr;
aInit(arr,int,0);
aAdd(arr,0);
aAdd(arr,1);

aEach(fastArr,i) {
    printf("item @ %d = %d\n",i,arr-&gt;data[i]);
}

aFree(arr);
See
this Stackoverflow answer for
a discussion on the optimal growth factor for dynamic
arrays. The gist of it is that it depends, but growth
factors around the golden ratio are easier for the memory allocator to
handle. I'd recommend using 1.5 because it is easy to implement:

DEST-&gt;cap += DEST-&gt;cap / 2


It likely fares worse on artifical benchmarks but better on real
workloads. This requires that the minimum capacity of the dynamic
array is 2 which is reasonable. But a more realistic minimum size
likely should be 8 or 16 since
the minimum malloc size is 32
bytes.

In aAppend you are always realloc:ing which will cause performance
to suffer if many small dynamic arrays are collected into one big one
in a loop. Instead, you want something like:

size_t req = DEST-&gt;size + X-&gt;size;
if (req &gt; DEST-&gt;cap) {
    aResize(DEST);
}
memcpy(DEST-&gt;data + DEST-&gt;size, X-&gt;data, X-&gt;size * X-&gt;typeSize);
DEST-&gt;size = req;


The last thing I'd change is the alignment of the capacity to a power
of two in aInit. On realistic workloads, a good chunk of all
variable arrays never grow so the alignment just wastes space. But
if you insist on alignment, this function is better than the loop:

static inline int
next_pow2(int v) {
    v--;
    v |= v &gt;&gt; 1;
    v |= v &gt;&gt; 2;
    v |= v &gt;&gt; 4;
    v |= v &gt;&gt; 8;
    v |= v &gt;&gt; 16;
    v++;
    return v;
}


I also wonder why you are using macros when C99 has inline functions?
They have many advantages in comparison to code macros.