2

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.

mocode10
  • 540
  • 3
  • 18
  • There is a school of thought that says : whenever you are using `reinterpret_cast` you are probably doing something wrong and need to rethink your design. – Ron Oct 05 '17 at 07:33
  • You're right that does accomplish the same thing as &(*pObj) – mocode10 Oct 05 '17 at 07:36
  • I"m confused why though, wouldn't that take the address of the pointer rather than the address of the object the pointer points to? – mocode10 Oct 05 '17 at 07:37
  • Whenever you need to cast something chances are you need to rethink the design. Except for the simple `static_cast` of built-in types. – Ron Oct 05 '17 at 07:37
  • Agreed but in this case it's for simplicity when adding bytes to an address using unsigned char since they are one byte on most machines – mocode10 Oct 05 '17 at 07:40

2 Answers2

0

It's because that you are passing pointer to a pointer. The address of an address, any changes will be held locally in the scope of function only. For solving this you should pass the pointer with reference.

void doWork(Obj* &pObj)

This should work.

Saram ali
  • 224
  • 1
  • 14
  • I vaguely get what you're saying. But could you show me a simple example on how the syntax would be to fix this? – mocode10 Oct 05 '17 at 07:35
  • @mocode10 https://stackoverflow.com/questions/10240161/reason-to-pass-a-pointer-by-reference-in-c Check this link – Saram ali Oct 05 '17 at 07:41
  • "You would want to pass a pointer by reference if you have a need to modify the pointer rather than the object that the pointer is pointing to." – mocode10 Oct 05 '17 at 07:42
  • I want to modify the object the pointer points to not the pointer in my case, I'm still not having any luck. – mocode10 Oct 05 '17 at 07:42
  • I'm confused why the way the pointer is passed in matters at all honestly as I'm only using that pointer to get the address then I go and create a new pointer using reinterpret cast with that address and do my work on that newly created pointer. – mocode10 Oct 05 '17 at 07:44
0

The (&(*pObj)) is redundant. You are taking the address of an object you just dereferenced. Surely (&(*pObj)) == pObj?

The most likely problem is your offset calculation. Your calculation adds the size of Obj and 16. We cannot tell from your example if this is correct because you don"t give us details of Obj or how the secret pointer is defined. If this calculation is wrong, then the update to value will appear to have worked in your function but you have changed the wrong bytes so it is not seen correctly outside the function.

Justin Finnerty
  • 329
  • 1
  • 7
  • It's not my offset calculation as I have checks in there to verify it's not garbage data in the secret object. The modification goes just fine. It just isn't preserved after function returns. Also I'm confused on why pObj would not give the address of the pointer rather than the address of the object the poitner points to? – mocode10 Oct 05 '17 at 07:56
  • TO be fair, I have changed my code to remove the redundancy and it works the same, just looking for an explanation why it works the same though :) – mocode10 Oct 05 '17 at 07:56
  • Without more details of your code, I can only suggest you run your code in a debugger and look at the memory contents. Your code updates a memory location so it will be altered after the function ends. Therefore it only seems reasonable to guess you are altering the wrong memory location. – Justin Finnerty Oct 05 '17 at 08:04