I have a function that takes two pointers as parameters, each points to a beginning of an array. The two arrays are of two different classes. The items of the first array (let's call is src (source)) actually form a list: each item has "next" and "previous" pointers. During the execution of the function items are popped out of the list. Each item of src has its own item in the other array, let's call it dst (destination). So in the function I need to be able to get the dst element belonging to the src element pointed by a given pointer. I found three methods:
1. Each element of src will have its index as a member (since dst[n] belongs to src[n] )
2. Element of src will have a pointer to its dst element
3. A weird method.
If the 3rd one works, it will probably be the best: saves memory and is the fastest, but I'm not sure it is possible. The idea is to save the difference between the pointers and get a pointer to the dst element by adding the pre-calculated difference to a given pointer to a src element.
The problem is that in order to be the fastest method (memory isn't a problem in my specific case) it needs to be able to save a negative difference without using if, abs() or any strange calculations. Method no. 1 is almost as fast. I'm trying the third because I'd like to know if it works. The answer will tell me a lot about pointer arithmetics. It's very important, since I'm just a beginner...
The problem with no. 3 is that if pointers are 4 bytes long, int can't save any difference. It will be enough only as long as the difference is at most half of the maximum. If it works, the initialization code will look like this:
class ClassOfDst;
class ClassOfSrc;
int diff = (unsignedint)dst - (unsignedint)src;
And this will be how you get member x of the correct dst element (ptr points to a src element)
theChosenX= ( (ClassOfDst*)((char*)ptr+diff) ) -> x ;
This line type-casts ptr to char* so diff bytes are added to ptr, giving a pointer to the correct dst element. Now ptr is casted to dst's type and the members are accessible.
But can a program have two arrays with a difference two big for int? And if yes, is the above line correct?
I'll first answer your last question: there's no guaranteed relation between the size of pointers and the size of non-pointers. An int may be as small as a char even if a int * is 64 bits long.
I'm not sure what you're trying to do, but I don't think it's safe.
I also don't know what your speed requirements are, but I think you're overestimating the performance downgrade a simpler and more robust solution would create.
A simpler solution is almost as fast: probably just one more ASM line for that line of code. It's fast enough anyway. But would still like to know whether the strange method works. Even if I don't use it. And I'm assuming that sizeof(int) is 4 and sizeof(int*) is 4 too.
Problem solved: this method doesn't work unless the classes have the same object size. For example, if src starts at address 1000 and dst starts at 2000, assuming sizeof(ClassOfSrc)==5 and sizeof(ClassOfDst?)==7 , the difference between src[1] and dst[1] is 1002. Not 1000, which is the original difference. And adding calculations that could solve the problem is only worth it if memory is extremely limited. So I'll just save indices as members...They don't change so I only need to initialize them once. By the way, everything in the weird method worked (when I used reinterpret_cast) except that problem with the difference calculation.
Thanks for the help :)