первый случай, они освобождаются в цикле:
struct entry
{
int utf8length;
char * utf8text;
};
...
for( int i = 0; i < constant_pool_count; i++ )
{
struct entry * ent;
char * carr;
ent = malloc(sizeof(struct entry));
if(ent==NULL)
{
fprintf(stderr, "Failed malloc, Exiting the program \n");
exit(-1);
}
ent->utf8length = sizeOfUtf;
carr = malloc(sizeof(char) * sizeOfUtf + 1);
if(carr==NULL)
{
fprintf(stderr, "Failed malloc, Exiting the program \n");
exit(-1);
}
ent->utf8text = carr; // I suppose
//More code
free(ent->utf8text);
free(ent);
}
второй случай, вам нужно, чтобы они вышли из цикла, затем вы освобождаете их
struct entry
{
int utf8length;
char * utf8text;
};
...
struct entry * all[constant_pool_count];
for( int i = 0; i < constant_pool_count; i++ )
{
struct entry * ent;
char * carr;
ent = malloc(sizeof(struct entry));
if(ent==NULL)
{
fprintf(stderr, "Failed malloc, Exiting the program \n");
exit(-1);
}
ent->utf8length = sizeOfUtf;
carr = malloc(sizeof(char) * sizeOfUtf + 1);
if(carr==NULL)
{
fprintf(stderr, "Failed malloc, Exiting the program \n");
exit(-1);
}
ent->utf8text = carr; // I suppose
all[i] = ent;
//More code
}
... some code
// free them
for( int i = 0; i < constant_pool_count; i++ )
{
free (all[i]->utf8text);
free (all[i]);
// may be "all[i] = NULL;" if useful ?
}