I have a pointer ***resultSet that I pass as parameter to my SQL function which reads an unknown amount of data from the DB. The actual handling of the data happens outside the function.
When cleaning up this pointer construct, do I just free the main pointer once and free finds all the subsequent allocations or do I have to free up every malloc I created?
while((row = mysql_fetch_row(result))) {
// allocating the rows pointer
resultSet = malloc(sizeof(void)*(int)mysql_num_rows);
(*rows)++;
for (i=0 ; i < mysql_num_fields(result); i++)
{
// allocating the fields pointer
*(resultSet+i) = malloc(sizeof(void)*(int)mysql_num_fields);
// allocating the character pointer
**resultSet = malloc(sizeof(char)*strlen(row[i])+1);
(*fields)++;
snprintf(**resultSet, strlen(row[i])+1, "%s", row[i]);
printf("%s\t",**resultSet);
if (i==6)
{
printf("\t %s\n",**resultSet);
}
}
}
As a general rule, for every
malloc()you have afree().