The question: Can the output of the below program be predicted reliably, given the full code for B
and C
?
If the answer is "no" (e.g. due to platform dependence), then: Is there a way to make it predictable (e.g., by using certain allocation/alignment techniques).
struct B{
// ...
};
struct C{
// ...
};
struct A{
B b;
C c;
};
int main(){
A a;
long db = (int*)(&a.b) - (int*)(&a );
long dc = (int*)(&a.c) - (int*)(&a.b);
std::cout << "difference a.b to a : " << db << "\n";
std::cout << "difference a.c to a.b : " << dc << "\n";
}
Remark(s):
- The use of
int*
is just because cpp disallows use ofvoid*
afaik. - I intend to eventually use
db,dc
to compute the address ofb
andc
from a given object ofA
at another compile time.