Fra*_*lea 0 c malloc free pointers
我有一个指针***resultSet,我将其作为参数传递给我的SQL函数,该函数从数据库中读取未知数量的数据.数据的实际处理发生在函数之外.
清理这个指针构造时,我只需释放一次主指针,free就可以找到所有后续分配,还是我必须释放我创建的每个malloc?
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)