1

i need to loop through a memory region which contains my own defined types:

struct ModuleList 
{
    char ModuleName[64];
    int32_t NumberOfFunctions;

};

struct FunctionList
{
    char FunctionName[64];
    DWORD FunctionAddress;
};

These lays out in memory like this:

0: ModuleList[0]
0 + sizeof(ModuleList): FunctionsList[NumberOfFunctions]
0 + sizeof(ModuleList)+ NumberOfFunctions * sizeof(FunctionList): ModuleList[1]
.
.
.

What i want to do is, overloading operator ++ for ModuleList* so i can easily increment my pointer correctly, because default ++ operator for pointer only increments for sizeof(ModuleList) and i want to increment for sizeof(ModuleList) and size of functions (which is NumberOfFunctions * sizeof(FunctionList)). These structs in memory are completely dynamic, which my program gets over the network. Currently, i am doing this:

unsigned short ModuleCount = 0;
BYTE* pTemp = (BYTE*)MemoryAdr;
for (; std::string(((ModuleList*)(pTemp))->ModuleName).find(".dll") != std::string::npos; pTemp += ModuleListSize + ((ModuleList*)(pTemp))->NumberOfFunctions * FunctionListSize)
    ModuleCount++;

But i think it looks ugly. Any suggestions?

6
  • 1
    You cannot overload operators for raw pointers - you would have to create a smart pointer class. Commented Jul 18, 2018 at 10:01
  • 1
    I'm not going to sugarcoat this: Overloading the increment operator(s) for custom pointer aliases will somehow bring clarity to your code? How about we old-school it and walk the structures in the loop, documenting what you're actually doing along the way rather than trying to misuse a feature that won't do anything but obfuscate the code further (if it worked, which it won't). Commented Jul 18, 2018 at 10:04
  • You can improve readability by adding a method void next() or similar to ModuleList, and call that in your foor loop Commented Jul 18, 2018 at 10:05
  • Iterating over heterogeneous content with a single pointer, smart or raw, doesn't make sense. Commented Jul 18, 2018 at 10:05
  • Partially, I think the issue is with terminology. Had you called it an iterator, then we would all be happy. An iterator is an object that can be used like a pointer in certain ways, and if you follow that pattern I think you can do what you want with reasonable clarity. Commented Jul 18, 2018 at 10:15

1 Answer 1

2

C++ requires that your operator overloads take at least one operand of a "class type" or enumeration type. So, you can't overload operator for intrinsic/POD types and you need to wrap your struct with self-made smart-pointer class (as Neil proposed).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.