[Solved] How to free() my variables [closed]


You can introduce another parameter to count the elements in freeme.

bool parse(bool b1, int i, int *j, int *e, char **ptr, int q, char **pString, char *str,
           char **freeme, int *freeme_len) {

    for (*e = 0; *(ptr + q + *e); (*e)++) {
        b1 = true;
        if (*(ptr + *e + q)) {
            str = concat(*pString, *(ptr + *e + q));
            *pString = concat(str, " ");
            free(str);
            //freeme[*e] = *pString;
            freeme[(*freeme_len)++] = *pString;
        }
        *j = *e;
    }
    return b1;
}

After calling parse() you iterate all freeme elements and free them.

void main() {
  //The following line outlines that freeme is allocated somewhere and it's type is char**
  //and should not be taken literally. It's size should be calculated, according 
  //to the situation, or should be dynamically resized during operations.
  char **freeme = malloc(1000 * sizeof(char*));

  //freeme_len should be initialized to 0 before first calling to parse().
  int freeme_len = 0;

  //here follows some program code, that we don't know about, which declares and 
  //initializes all remaining variables
  parse(b1, i, &j, &e, ptr, q, &pString, str, freeme, &freeme_len);

  //After calling, free all
  for(i=0; i<freeme_len; i++) {
    free( freeme[i] );
  }

  free(freeme);
}

3

solved How to free() my variables [closed]