C99/C11 dynamic array that mimics C++'s std::vector
This sort of approach is a pain to use, because you keep having to cast when you&#x27;re in the debugger, and there&#x27;s zero type safety. And I&#x27;m afraid I don&#x27;t have much positive to say about something like &quot;((Vector2i * )arr3-&gt;data)[0].x = 333&quot;.<p>You can do better than this!<p>What is an array? It&#x27;s 3 variables: base, length and capacity. So why not decide that an array is just that. 3 variables of the right size and type.<p><pre><code>    #define ARRAY(T,S) T S;size_t S##_length;size_t S##_capacity
</code></pre>
Then you can make one like this:<p><pre><code>    ARRAY(int,xs);
</code></pre>
You&#x27;ll also need to initialise and these destroy array &quot;objects&quot;.<p><pre><code>    #define ARRAY_INIT(S)     \
        do {                  \
            S=NULL;           \
            (S##_length)=0;   \
            (S##_capacity)=0; \
        } while(0)

    #define ARRAY_DESTROY(S) \
        do {                 \
            Array_Free(S);   \
            ARRAY_INIT(S);   \
        } while(0)

    </code></pre>
Add you&#x27;ll probably want to add an item to an array too.<p><pre><code>    #define ARRAY_ADD(S,X)                     \
        do {                                   \
            if((S##_length)&gt;=(S##_capacity)) { \
                S=Array_Grow(S,                \
                             sizeof *S,        \
                             &amp;(S##_length),    \
                             &amp;(S##_capacity)); \
            S[S##_length++]=(X);               \
        } while(0)
</code></pre>
So you might use them like this:<p><pre><code>    ARRAY(int,xs);
    ARRAY_INIT(xs);
    for(int i=0;i&lt;100;++i)
        ARRAY_ADD(xs,i);
    ARRAY_DESTROY(xs);
</code></pre>
Array_Free is very simple, and Array_Grow is barely more complicated (however I wrote it off the cuff, so of course it could still be wrong). Both of these mainly exist just to keep stdlib.h out of the header.<p><pre><code>    void Array_Free(void *p) {
        free(p);
    }

    void *Array_Grow(void *base,size_t stride,size_t *length,size_t *capacity) {
        *capacity+=*capacity&#x2F;2;
        *capacity=MAX(*capacity,MAX(MIN_CAPACITY,*length));
        return realloc(base,*capacity*stride);
    }
</code></pre>
Array accesses and iteration and the like are just done in the traditional way:<p><pre><code>    for(size_t i=0;i&lt;xs_length;++i) {
        printf(&quot;%d\n&quot;,xs[i]);
    }
</code></pre>
Even performs nicely with -O0.<p>For a full implementation you&#x27;ll probably also need a way of generating a static array. (I mainly found myself needing this for test code, which uses globals for convenience; most arrays I create normally are locals, or parts of structs.)<p>You&#x27;ll also need a parameters list for use in a function declaration or definition, and a macro that expands to all 3 variables.<p><pre><code>    #define ARRAY_PARAMS(T,S) T *S,size_t S##_length,size_t S##_capacity
    #define ARRAY_ARG(S) S,S##_length,S##_capacity
</code></pre>
Like then you might have a function that takes a pointer to an &quot;array&quot;:<p><pre><code>    void FunctionThatTakesAnArray(ARRAY_PARAMS(T,*p));
</code></pre>
And you call it like this:<p><pre><code>    ARRAY(T,myarray);
    FunctionThatTakesAnArray(ARRAY_ARG(&amp;myarray));
</code></pre>
(I found this cropped up often enough that I needed the macro, but it was less common than I thought.)<p>There&#x27;s more you can do, but the above is the long and the short of it.<p>This might all look terrible - or perhaps it sort of looks OK, but you&#x27;re just not sure that it would actually work - but I&#x27;ve used this in a prototype project and thought it worked out well. (I&#x27;ve been using C for 20+ years, so hopefully even if I&#x27;ve got no taste, I&#x27;ve at least got a rough feel for what works out OK and what&#x27;s going to end up a disaster.)