I am writing a program in which I have to manage an array of strings. Throughout the program, the strings are systematically removed/modified until only a single string remains.

I remove the strings when they are no longer needed using this function:

```
void remove_element(char **array, int index, int array_length)
{
    int i;
    for(i = index; i < array_length - 1; i++){
        array[i] = array[i + 1];
    }
    free(array[array_length]);
}

```

Once I am totally done with the array, I use this function to ensure all the strings are free'd:

```
void free_fragments(char **frags){
    int i = 0;
    while((frags[i] != NULL) && sizeof(frags[i] != 0)){
        free(frags[i]);
        i++;
     }
    free(frags);
}

```

Yet, when I do this, valgrind gives me an "Invalid free() / delete / delete[] / realloc()" error, specifically complaining about the "free(frags[i])" line in free\_fragments.

I understand that free\_fragments seems kind of redundant, simply because theoretically I can call remove\_element on each element until there is one remaining and then remove\_element that as well, but when I try to do that, I still get a Invalid free() error, but this time complaining about remove\_element. If I don't call free\_fragments when I am done with the array, I get a memory leak.

My understanding of pointers is pretty beginner, so please help me debug this and improve my understanding!