F
Frederick Gotham
There is a thread currently active on this newsgroup entitled:
"how to calculate the difference between 2 addresses ?"
The thread deals with calculating the distance, in bytes, between two
memory addresses. Obviously, this can only be done if the addresses refer
to elements or members of the same object (or base objects, etc.).
John Carson and I proposed two separate methods.
I disagree with John's solution, and John disagrees with mine. Therefore,
I'd like to present them both here and see what the audience thinks.
Firstly, we shall start off with a simple POD type:
struct MyPOD {
int a;
double b;
void *c;
short d;
bool e;
int f;
};
Given an object of this type, we shall calculate the distance, in bytes,
between the "b" member and the "e" member.
My own method is as follows:
reinterpret_cast<char const volatile*>(&obj.e)
- reinterpret_cast<char const volatile*>(&obj.b)
John's method is as follows:
reinterpret_cast<long unsigned>(&obj.e)
- reinterpret_cast<long unsigned(&obj.b);
In defence of my own method:
(1) Any byte address can be accurately stored in a char*.
In attack of John's method:
(1) The Standard doesn't necessitate the existance of an integer type
large enough to accomodate a memory address.
(2) Even if such a type exists, the subtraction need not yield the
correct answer (e.g. if each integer 1 represents half a byte, or a quarter
of a byte).
Of course, seeing as how _I_ started this thread, it may be a little biased
toward my own ends, but I hope we get to the bottom of this objectively.
"how to calculate the difference between 2 addresses ?"
The thread deals with calculating the distance, in bytes, between two
memory addresses. Obviously, this can only be done if the addresses refer
to elements or members of the same object (or base objects, etc.).
John Carson and I proposed two separate methods.
I disagree with John's solution, and John disagrees with mine. Therefore,
I'd like to present them both here and see what the audience thinks.
Firstly, we shall start off with a simple POD type:
struct MyPOD {
int a;
double b;
void *c;
short d;
bool e;
int f;
};
Given an object of this type, we shall calculate the distance, in bytes,
between the "b" member and the "e" member.
My own method is as follows:
reinterpret_cast<char const volatile*>(&obj.e)
- reinterpret_cast<char const volatile*>(&obj.b)
John's method is as follows:
reinterpret_cast<long unsigned>(&obj.e)
- reinterpret_cast<long unsigned(&obj.b);
In defence of my own method:
(1) Any byte address can be accurately stored in a char*.
In attack of John's method:
(1) The Standard doesn't necessitate the existance of an integer type
large enough to accomodate a memory address.
(2) Even if such a type exists, the subtraction need not yield the
correct answer (e.g. if each integer 1 represents half a byte, or a quarter
of a byte).
Of course, seeing as how _I_ started this thread, it may be a little biased
toward my own ends, but I hope we get to the bottom of this objectively.