deleting an element in a class array

Brief Background:
- I am using two classes (Patient and List) + test drive (.cpp file)
- Here is a peek at my private section of List class:
- Purpose of program: creating a walking clinic system which the user can add, remove, and search patients.

1
2
3
static const int MAX_ELEMENT = 5;      
Patient elements[MAX_ELEMENT];     // an array of Patient class
int elementCount;


I am using an array of Patient class. I want to delete a specific patient, does delete [] elements[i] work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool List::remove(Patient &targetElement){
	if(elementCount > 0){
		if(search(targetElement) == true){
			for(int i = 0; i < elementCount; i++){
				if(elements[i] == targetElement){
					delete [] elements[i];
                                        --elementCount;
					return true;
				}
			}
		}
	}
	return false;
}



or ...

should I shift the elements in the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool List::remove(const Patient& toBeRemoved){
	for (int i = 0; i < elementCount; ++i) {
		if (elements[i] == toBeRemoved) {

			for (int fromIndex = i+1, toIndex = i ; fromIndex < elementCount; fromIndex++, toIndex++){
				elements[toIndex] = elements[fromIndex];
			}
			
			elementCount--;
			return true;
		}
	}
	return false;
}

Last edited on
> does delete [] elements[i] work?

No.


> should I shift the elements in the array?

Yes.

Alternatively, if the relative order of the remaining elements need not be preserved:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool List::remove( const Patient& toBeRemoved ) {

    for (int i = 0; i < elementCount; ++i ) {

        if( elements[i] == toBeRemoved ) {

            // swap this element with the element at the back of the list
            using std::swap ;
            swap( elements[i], elements[elementCount-1] ) ;

            --elementCount ; // and decrement the count of elements
            return true ;
        }
    }

    return false;
}

I want to delete a specific patient, does delete [] elements[i] work?


delete (or delete[]) means "this item that I got using new (or new[]); call the destructor on that item and after that I don't care at all what happens to that memory".

It has nothing - nothing - to do with removing elements from arrays or anywhere else. If you didn't use new, you shouldn't be using delete.

In summary, delete does NOT delete anything.
> I want to delete a specific patient, does delete [] elements[i] work?

>> In summary, delete does NOT delete anything.

In summary, delete[] elements[i]; would generate a compile-time error.

Note: delete[] std::addressof( elements[i] ) would compile cleanly.
It would engender undefined behaviour; there is no telling what it may or may not do.
Thank you everyone for the help! Your information really made it clear.

Therefore, using an array, we have to shift the elements over to the index that needs to be deleted. In this case, we write the unwanted element with other wanted elements.


As a simple alternative, rather than using an array, use a vector (I seem to say this a lot). Vector comes with an erase function that works how you would expect; the element vanishes and the others move along one to fill in the gap. The STL is there to help you
vectors are sweet but you may want to avoid resizes and big data shifts same as an array, and erase shifts the data every time.

If you have data that inserts and deletes constantly consider adding a 'isdeleted' boolean to what you put into your vector.
when deleted, set it to true. Constructor can set it to false default. When getting data out, if its deleted, treat that entry as if not there.

Once in a while (depending on how fast your data changes) you can do a cleanup function that copies records where the data is not deleted into a new vector, dropping the deletes all at once. You can keep a running counter every time something is deleted and when that reaches, say, 10% of the current size, run the cleaner.

This works for arrays too, but arrays have a lot of issues besides this, making vectors the better way for most places where an array would be used.
Last edited on
Topic archived. No new replies allowed.