I am trying to understand some code that looks like this:
node* temp = (marker*)(ptr);
node* holder = *((&(temp)) + (-1));
This appears to be subtracting from a memory address.
Would it be equivalent to: node* holder = temp->prev;
?
I am trying to understand some code that looks like this:
node* temp = (marker*)(ptr);
node* holder = *((&(temp)) + (-1));
This appears to be subtracting from a memory address.
Would it be equivalent to: node* holder = temp->prev;
?
&(temp) is the memory address where temp is stored. This is a local variable in the current function so it's on the stack.
Adding (-1) is the same as subtracting 1. How this behaves is implementation-dependent. That said, it either returns the next address on the stack - in this case the address of holder - or the previous address on the stack - which we can't see what it is from the rest of the code; depending on the situation it might even be one of the parameters passed to the function.
Either way as the commenters indicated the fact that this code works at all appears to be an accident.
It would not be the equivalent of temp->prev;
If each node is allocated independently (i.e. not in an array), then the memory address's could be spaced very far apart. Thus subtracting one from the memory address is never ever in a million years a good way to get the previous item in the list.
That code is something a new programmer like yourself should not be dealing with, nor writing. Sheesh, I've had 15 years of C++ under my belt, and I look at that and sigh...