1
\$\begingroup\$

A litle context:

This game server is coded entirely on C-with-classes (plain C code using basic C++ features like classes, templates and such, to avoid any possible overhead). At startup the server allocates a big block of memory to hold an awful lot of objects (talking about millions of objects! the map is quite big). I've designed this Object class:

class Object {
    Object* NextObject; // Pointer to first item in our same container (tiles are Map Containers). Can be NULL.
    Object* Container;  // Cannot be NULL, except if we are a Map Container object.
    Object* Content;    // Pointer to our content, if we have, otherwise NULL.
    ObjectType* Type;   // A C array holds all the object types, this points to ours
    int32_t InstanceAttributes[20]; // Can be plain integers or some sort of reference to other object (i.e the Dynamic String Table).
    bool State;

};

Object::Object() {
    State = 0;
    ObjectTableFree.push_front();

}

I decided to use a chunked arrays to store my objects, this way:

#define TABLESIZE 64
#define CHUNKSIZE 32768

ObjectChunk* ObjectTable;
std::forward_list<Object *> ObjectTableFree(TABLESIZE * CHUNKSIZE);

struct ObjectChunk {
    Object Obj[CHUNKSIZE];
};

void InitMap() {
    ObjectTable = new ObjectChunk[TABLESIZE];

    // Here we also read the map from the hard disk, and 
}

These tiny "Helpers" as I call them will happily carry out the Object logic. (Of course I will not include them all, these are just so you can get an idea).

Object* CreateObject() {
    if (ObjectTableFree.empty())
        ResizeObjectTable();

    Object* obj = ObjectTableFree.front();
    assert(obj);

    ObjectTableFree.pop_front();
    return obj;
}

Object* AppendObject(Object* Con, ObjectType* Type, bool AsContent) {
    assert(Con);

    Object* obj = CreateObject();
    obj->Container = Con;
    Object->setType(Type);

    if (AsContent) {
        if (Con->Content)
            obj->NextObject = Con->Content;

        Con->NextObject = obj;
        return obj;

    if (Con->NextObject)
        obj->NextObject = Con->NextObject;

    Con->NextObject = obj;
    return obj;

}

When my map loads I append the objects to the map containers and start manipulating them through pointers through the whole code. Please note that even though this is a C++ project, I can't use most of its features because of internal convention.

  • Is this all-pointer design a smart one?
  • How could this be redesigned to make it more efficient/faster?.
  • Is there any way to accomplish this without all those pointers?
\$\endgroup\$

closed as off-topic by πάντα ῥεῖ, user673679, Edward, dfhwze, pacmaninbw Jul 17 at 21:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, user673679, Edward, dfhwze, pacmaninbw
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Take a look at allocators, and placement new. \$\endgroup\$ – πάντα ῥεῖ Jul 17 at 17:57
  • 3
    \$\begingroup\$ This question is incomplete. To help reviewers give you (better) answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does. In this particular case, we are missing a scenario where you show us how your code can be used. Unit tests wouldn't hurt either. \$\endgroup\$ – dfhwze Jul 17 at 18:30