07-26-2023, 11:26 PM
I have read that if you declare two structs like this:
struct Node {
int a, b, c;
};
struct DerivedNode {
struct Node base;
int d, e, f;
};
Then you can use pointers to them like this:
struct DerivedNode myDerivedNode;
struct Node *regularNode = (struct Node *) &myDerivedNode;
regularNode->a = 3;
In other words, the address offsets for `a, b, c` are the same within `struct Node` and `struct DerivedNode`. So you can get a kind of polymorphism out of it, where you can pass in a forcibly `(struct Node *)`-cast DerivedNode pointer wherever a Node pointer would normally be taken.
My question is whether this behavior is guaranteed. I know there are some weird memory alignment issues and that the compiler sometimes reorders fields to achieve better packing in memory. Will the `base` field ever be located anywhere but the beginning of `struct DerivedNode`?
struct Node {
int a, b, c;
};
struct DerivedNode {
struct Node base;
int d, e, f;
};
Then you can use pointers to them like this:
struct DerivedNode myDerivedNode;
struct Node *regularNode = (struct Node *) &myDerivedNode;
regularNode->a = 3;
In other words, the address offsets for `a, b, c` are the same within `struct Node` and `struct DerivedNode`. So you can get a kind of polymorphism out of it, where you can pass in a forcibly `(struct Node *)`-cast DerivedNode pointer wherever a Node pointer would normally be taken.
My question is whether this behavior is guaranteed. I know there are some weird memory alignment issues and that the compiler sometimes reorders fields to achieve better packing in memory. Will the `base` field ever be located anywhere but the beginning of `struct DerivedNode`?