I have an array of pointers to structs, and what I want to do is delete an element and shift all the rest to fill the gap. I have written a function which seems to work, however valgrind complains about 'invalid read/write of size 8' so now I'm wondering if what I did was incorrect.

Here's the code:

```
for (int i = (numOfApartments-1); i >= 0; i--) {
    if (apartmentIsIdentical(apartment, apartmentArray[i]->apartment)) {
        apartmentDestroy(apartmentArray[i]->apartment);
        free(apartmentArray[i]);
        shiftApartments(apartmentArray, i, numOfApartments);
        numOfApartments--;
        return 1;
    }
}

static void shiftApartments(ApartmentInfo* array, int startIndex, int endIndex) {
    for (int i = startIndex; i < endIndex; i++) {
        swapApartments(&array[i], &array[i + 1]);
    }
}

static void swapApartments(ApartmentInfo* apartment1, ApartmentInfo* apartment2) {
    ApartmentInfo temp = *apartment1;
    *apartment1 = *apartment2;
    *apartment2 = temp;
}

```

My question is mainly whether the free(apartmentArray[i]) is correct. The way I see it, it should leave an empty slot which I just move to the end of the array and place something else there later. However the error of 'invalid read/write of size 8' made me wonder whether freeing apartmentArray[i] actually make the slot inaccessible..? Why does it say I can't write to it?

Thanks!