Dynamic array of structs in C
I am also concerned if there can be situation that I free a pointer twice in my case


... and ...


  Yes just I am more interested in cases when/if there can be for example freeing unallocated memory or freeing already freed memory ...


After a quick inspection it doesn't appear that you free memory more than once:


free statements are only in the freeArray and main methods
Each free(a-&gt;array[0].name); is different because each name is allocated using its own malloc
free(a-&gt;array) is only called once
freeArray is only called once
free(x.name); doesn't free the same memory as free(a-&gt;array[0].name); because insertArray allocates new memory for each name



  and how to avoid that


Something which can help (though not guarantee) is to assign NULL to the pointer after you pass it to free.


It can help, because calling free on a previously-nulled pointer will harmlessly do nothing
It's not a guarantee, because you might have more than one pointer pointing to the same memory




dmcr_code's comment below points out a bug. You wrote,

for(int i=0; i&lt;a-&gt;used; i++)
{
    free(a-&gt;array[0].name);
    a-&gt;array[0].name=NULL;
}


This should be,

for(int i=0; i&lt;a-&gt;used; i++)
{
    free(a-&gt;array[i].name);
    a-&gt;array[i].name=NULL;
}


Because you set a-&gt;array[0].name=NULL; after freeing it, you don't free it twice.

But, you did fail to free the memory associated with a-&gt;array[i].name for values of i larger than 0.




  But then how do I protect against that - when array[i].name can contain random value and I try to free it?


To protect yourself:


Either, don't let it contain a random value (e.g. ensure that it's either a valid pointer, or zero)
Or, don't use it (e.g. ensure that your a-&gt;used logic is correct so that you don't touch elements which you haven't used/initialized).



  is memset in the initArray method fine for that?


memset is good:


You could use calloc instead of malloc to avoid having to use memset as well
You could use memset on the whole array at once instead of using memset on each element of the array


memset in initArray isn't enough. It's enough to begin with, but there's a realloc in insertArray. So to be good enough, you'd also need to use memset after realloc (to memset the as-yet-unused end of the newly-reallocated array; without using memset on the beginning of the reallocated array, which already contains valid/initialized/used elements).


  the only unclear part that remains from your response is how to memset realloced array


Your current code in initArray says,

// Initialize all values of the array to 0
for(unsigned int i = 0; i&lt;initialSize; i++)
{
    memset(&amp;a-&gt;array[i],0,sizeof(Student));
}


Another way to do that would be:

// Initialize all elements of the array at once: they are contiguous
memset(&amp;a-&gt;array[0], 0, sizeof(Student) * initialSize);


The memset statement to add to insertArray would be:

if (a-&gt;used == a-&gt;size)
{
    a-&gt;size *= 2;
    a-&gt;array = (Student *)realloc(a-&gt;array, a-&gt;size * sizeof(Student));
    // Initialize the last/new elements of the reallocated array
    for(unsigned int i = a-&gt;used; i&lt;a-&gt;size; i++)
    {
        memset(&amp;a-&gt;array[i],0,sizeof(Student));
    }
}


Or:

if (a-&gt;used == a-&gt;size)
{
    a-&gt;size *= 2;
    a-&gt;array = (Student *)realloc(a-&gt;array, a-&gt;size * sizeof(Student));
    // Initialize the last/new elements of the reallocated array
    memset(&amp;a-&gt;array[a-&gt;used],0,sizeof(Student) * (a-&gt;size - a-&gt;used));
}



  and this comment: "It's not a guarantee, because you might have more than one pointer pointing to the same memory " would be nice if you can address that too


This is safe:

void* foo = malloc(10);
free(foo);
// protect against freeing twice
foo = NULL;
// this is useless and strange, but harmless
free(foo);


This is not safe:

void* foo = malloc(10);
void* bar = foo;
free(foo);
// protect against freeing twice
foo = NULL;
// this is useless and strange, but harmless
free(foo);
// but this is dangerous, illegal, undefined, etc.
// because bar is now pointing to memory that has already been freed
free(bar);