You may want to consider doing the memory allocation in a single malloc()
rather than the piecewise approach you are using. For instance you might want to consider doing something like the following.
This allocates the memory needed in a single call to malloc()
so that only a single call to free()
is needed to release the memory. It is faster and tends to make the heap to be less fragmented and easier to manage.
typedef struct
{
uint32_t *bar[1]; // an array of size one that makes array syntax easy
} Foo;
Foo *FooCreateFoo (unsigned int nCount)
{
// create an array of pointers to uint32_t values along with the
// memory area for those uint32_t data values.
Foo *p = malloc (sizeof(uint32_t *) * nCount + sizeof(uint32_t) * nCount);
if (p) {
// we have memory allocated so now initialize the array of pointers
unsigned int iLoop;
uint32_t *pData = p->bar + nCount; // point to past the last array element
for (iLoop = 0; iLoop < nCount; iLoop++) {
// set the pointer value and initialize the data to zero.
*(p->bar[iLoop] = pData++) = 0;
}
}
return p;
}
int main(void)
{
Foo *foo = FooCreateFoo (20);
if (! foo) {
// memory allocation failed so exit out.
return 1;
}
// ... do things with foo by dereferencing the pointers in the array as in
*(foo->bar[1]) += 3; // increment the uint32_t pointed to by a value of 3
free (foo); // we be done with foo so release the memory
}