The setup is I have an object and below that object is a secret pointer.
This pointer points to an object of the same class but a different object than the one above it.
I have a function that takes in the top object and uses pointer arithmetic to get the pointer that is below it.
Then inside this function I want to modify the value of the object the secret pointer is pointing to.
In debug I can see the value is being modified just fine in the function but once the function returns the value is not preserved.
I'm beyond confused why.
Any ideas?
Also I own all the memory that these objects and pointers are being created in so I don't think any heap issues should occur as I'm doing my own little memory manager for fun.
I"m thinking the issue is related to me using reinterpret_cast, if I"m right what would be a solution/alternative, and why is this the issue?
void doWork(Obj* pObj) {
// Get address of the object the pointer is pointing to
unsigned char* position = reinterpret_cast<unsigned char*>(pObj);
// 16 Bytes below the object is a secret pointer
position += (sizeof(Obj) + 16);
// Retrieve the secret pointer
Obj* secretObj = reinterpret_cast<Obj*>(position);
// Modify a value in that secret object
secretObj->value += 1;
}
I have tried the suggestions of passing the pointer in by reference and still had no luck.
I'm confused why the way the pointer is passed in would even matter at all honestly as I'm only using that pointer to get the address to use as base then I go and create a new pointer using reinterpret cast with that (address + sizeof(Obj)) and do my work on that newly created pointer.