Here's one example:
#include <stdlib.h>
typedef struct auxiliarRegistre { ... } clients;
int arrSize = SOME_START_SIZE;
clients *arr = malloc( arrSize * sizeof *arr );
/**
* Do stuff with arr. When you need to extend the buffer, do the following:
*/
clients *tmp = realloc( clients, sizeof *arr * ( arrSize * 2));
if ( tmp )
{
arr = tmp;
arrSize *= 2;
}
Doubling the size of the buffer each time you need to extend it is a common strategy; this tends to minimize the number of calls to realloc. It can also lead to serious internal fragmentation; if you have 128 elements and you need to store just one more, you wind up allocating 256 elements overall. You can also extend by a fixed amount, such as
clients *tmp = realloc( clients, sizeof *arr * ( arrSize + extent ));
if ( tmp )
{
arr = tmp;
arrSize += extent;
}
Note that you don't want to assign the result of realloc directly to your buffer; if it returns NULL due to an error, you'll lose your reference to the memory you've already allocated, leading to a memory leak. Also, you don't want to update your array size until you know the call has succeeded.
malloccall allocates space for oneclientstructure. You might also want to read aboutrealloc.malloc(), which is wrong. Apart from that, are you looking forrealloc()?realloc()to embiggenPrimaryClientsas it is not allocated withmalloc().pointerbe of typeint*that is a pointer tointand not a pointer toclients,clients*?