This is a question about a passage from this article on component based engine design.
I suggest having every game object exist within a single giant array. This keeps game objects together in memory, and even though deletion of a game object is of O(n) complexity, that O(n) operation traverses an array, and usually will turn out to be unnoticeable. A custom vector or array class can be created that avoids the O(n) operation entirely by taking the element at the end, and placing it into the deleted slot.
I'm trying to understand what kind of operation the author is talking about. If I'm getting the overall point correctly, it's that accessing elements that are closer together in memory is faster, so therefor it's desirable to reorder the array such that there aren't any holes left when a game object is deleted.
If that is what the author means, then how is it possible to implement? This is what I'm imagining:
void Factory::addObject(const std::string name) {
objects.push_back(new GameObject(name));
void Facrory::removeObject(const std::string name) {
for(auto itr = objects.begin(); itr != objects.end()) {
GameObject* obj = *itr;
if(obj->name == name) {
delete obj;
// grab GameObject from end
GameObject* endObj = objects.back();
objects.erase(--m_objects.end());
itr = objects.insert(itr, endObj);
// else increment itr and continue
But that can't be right because just rearranging the pointers doesn't change how the memory is organized. So the author must be talking about something I don't understand about memory management.
My question is:
Have I misunderstood what the author meant? If not, then please explain how the author is suggesting the rearrangement is supposed to be done.
GameObject
before deleting it. That sounds very questionable and troublesome to me. \$\endgroup\$