It is useless at best and dangerous at worst.
2. Remember the size of the malloc 'ing.
char** inputArray = malloc(arrSize * (sizeof(char)));
This makes no sense and is probably random. Typically, the type that you are malloc 'ing and the pointer pointing to the resulting repository should differ only in one indirect. Ie:
char** inputArray = malloc(arrSize * sizeof(char*));
Best rule of thumb, let the compiler figure this out. sizeof can infer the type that it should measure from an expression.
char **inputArray = malloc(arrSize * sizeof(*inputArray));
This works because the sizeof operand is an invaluable context. The pointer will not actually be dereferenced, only its type will be displayed.
Note: sizeof parentheses are not needed around the expression, but I left them for clarity. Remove them as soon as you feel comfortable.
3. Verify that the selection was successful.
malloc and friends will return NULL in case of problems. You have to check it out.
4. Fix realloc
inputArray = realloc(inputArray, );
It is not right. As mentioned above, if realloc fails, it will return NULL and do nothing. This means that inputArray is still pointing to the previous store. That is, until you have exceeded this pointer with the just returned NULL realloc and leaked the repository mentioned. Unfortunately.
Always save, verify, and then assign the realloc result.
char **inputArray_ = realloc(inputArray, ); if(!inputArray_) { } inputArray = inputArray_;
source share