-1

I have a lot of structures inside a header file. I'm trying to think of a way to consolidate them. I know there has to be a better way to access these pointers. I used a program called reclass; what it does is create structures based off pointers and other data types the user selects. You take those structures and put them into your source code and use them. But as my program is so huge now. The way the structures are laid out. It is causing me a lot of headache.

Here is what I have (Version Number Example):

class Level1ToVersionNumer
{
    public:
        char _0x0000[8];
            char VersionNumber[12]; //0x0008 
        char _0x0014[44];
};//Size=0x0040

class Level0ToVersionNumber
{
    public:
    char _0x0000[20];
        Level1ToVersionNumer* Level1PointerToVersionNumber; //0x0014 
    char _0x0018[40];
};//Size=0x0040

class BaseObjectWorld
{
    public:
        char _0x0000[32];
            Level0ToVersionNumber* Level0PointerToVersionNumber; //0x0020 
        char _0x0024[1844];
            ObjectTable* ObjectTablePtr; //0x0758 
        char _0x0748[760];
            LocalPlayerLevel0* LocalPlayerLevel0Ptr; //0x0A54 
        char _0x0A44[1348];
            NearLootTable* NearLootTablePtr; //0x0F88 
            volatile __int32 SizeOfNearLootTable; //0x0F8C 
        char _0x0F90[160];
            FarLootTable* FarLootTablePtr; //0x1030 
            __int32 SizeOfFarLootTable; //0x1034    
};//Size=0x1840

You can see inside class Level0ToVersionNumber there is 20 bytes of padding.

Inside Level1ToVersionNumber there is 8 bytes of padding.

I currently am accessing these structures by:

g_pArmaMain->BaseObectWorld->Level0PointerToVersionNumber->Level1PointerToVersionNumber->VersionNumber;

As you can see this is 4 structures deep. Well some of my code is 15 to 20 stuctures deep. I know there has to be a better way of setting this up. This can't be the only way to do this.

Does anyone know how to shrink/consolidate the classes with multilevel pointers or some other creative way?

This is ridiculous when I have to do things like this:

if( !( g_pArmaMain->BaseObjectWorld->ObjectTablePtr->ObjectTableArrayPtr->ObjectTableArrayElement[i].element->EntityPtr->EntityPlus70->EntityPlus70Plus40->ObjectNamePtr <= 0 ) && !( g_pArmaMain->BaseObjectWorld->ObjectTablePtr->ObjectTableArrayPtr->ObjectTableArrayElement[i].element->EntityPtr->EntityPlus70->EntityPlus70Plus40->ObjectNamePtr->lengthOfEntName <= 0 ) )

It works but its just nasty nasty nasty...

Thanks for any help.

juan.facorro
  • 9,791
  • 2
  • 33
  • 41

1 Answers1

0

First of all, C and C++ are definitely not the same language. As a simple example, your code won't compile as C because there's no such thing as a class in C and there is no memory protection, so public/protected/private specifiers don't exist either.

There are a lot of differences beyond that C++ has additional features. It is not even a strict superset of C. However, I'll get off my soap box here and let you read up if you're curious: more info


To your question, your code doesn't provide a lot of info about what task its trying to accomplish, so I will not be able to offer concrete suggestions. I can offer some general thoughts and approaches to solving this problem though:

First, having a lot of nested data structures being referenced in a code block suggests to me that the function is trying to do more than it should. As a rule of thumb, you should strive to break your task down into small, functional pieces; write small, specialized functions that do simple things and then use those functions as building blocks to accomplish the larger task.

My second thought is to define methods in your classes. For example, if you need to get the version info from a BaseObjectWorld object, you write the method:

char* BaseObjectWorld::getVersion() { ... };

that traverses the BaseObjectWorld member hierarchy and returns the object's version information.

If you'd rather stick with C style coding, you'll want to convert those class definitions into struct definitions and then do a similar approach with functions:

char* get_version(struct BaseObjectWorld* instance) { ... };

The final thing I'd look at is whether certain structures actually need to be distinct structures. If there's no case where you'd be instantiating a Level1ToVersionNumber object separately from a Level0ToVersionNumber object, you might be able to roll those into a single object, and so forth. The key is to think about "what are my smallest functional building blocks for this task?" and then define your objects to match those.

Hope this is helpful. Cheers :)

awiseman
  • 5,539
  • 1
  • 18
  • 15