You should avoid global variables and clean up on failure. So, I might expect something like this:
Code:
stStruct **allocMyArray(size_t dimense)
{
stStruct **myArray = malloc(sizeof(myArray[0]) * dimense);
if (!myArray) {
return NULL;
}
for (size_t i = 0; i < dimense; i++) {
myArray[i] = malloc(sizeof(*myArray[i]));
if (!myArray[i]) {
for (size_t j = 0; j < i; j++) {
free(myArray[j]);
}
return NULL;
}
}
return myArray;
}
Having said that, do you really need a dynamic array of stStruct pointers, each of which points to a lone stStruct object rather than to the first object in a dynamic array of stStruct objects? This can be useful if you want to allow for "gaps" in the array such that an object can be removed from the array by setting the corresponding pointer to be a null pointer. But if not, it would be more efficient to simply create a dynamic array of stStruct objects rather than pointers.
You might also consider creating a struct to bundle the dynamic array with its dimension, e.g.,
Code:
struct stStructArray
{
stStruct **data;
size_t dimense;
};
This can help avoid mistakes where you have more than one such dynamic array, and then you mix up which dimension is for which dynamic array.