Reputation: 1447
I have a parent structure that contains a child structure within it, as well as a pointer to a union which points to the child. My problem is that the sample code below works, until I want to point my pointer to my parent structure to another instance of my parent structure at which point my internal pointers seems to break and cause segfaults. I can get around this by updating the internal pointers every time I change where the parent points but am hoping to avoid that. I thought that since internally the structure is not changing that the internal pointers would still be setup to point to the correct offset from the new parent address but it seems I am mistaken?
Can anyone help me out, below I have sample code (please disregard the pointlessness of my example, my situation is much more complex and there is a reason for this but the example illustrates my issue).
Here is a simplified version of my scenario:
typedef struct mychildstruct
{
uint16_t member
}mychildstruct;
typedef union myunion
{
mychildstruct child;
uint16_t a;
}myunion;
typedef struct myparentstruct
{
mychildstruct child;
myunion *union;
}myparentstruct;
myparentstruct parent;
myparentstruct *parent_ptr;
parent_ptr = (myparentstruct *)&parent;
parent_ptr->union = (myunion *)&parent.child;
No problems here and reading or writing to parent_ptr->union->child
works fine and correctly points to the same memory location as parent_ptr->union->a
. However if I do this:
myparentstruct parent2;
parent_ptr = (myparentstruct *)&parent2;
Then parent_ptr->union->child
and parent_ptr->union->a
segfault.
Is there a way around reassigning the correct memory address to parent_ptr->union
every time I change parent_ptr
?
Thanks for your help!
Upvotes: 0
Views: 139
Reputation: 6269
The answer to your question is: NO! there is no way around reassigning in your example.
It does not matter if you use use an absolute or relative pointer. Pointer arithmetic will not help you.
In your example, you allocate (statically, on the stack) 2 separate structures:
parent
and parent2
.
Each of them occupies separate memory areas, and in each there is a union
pointer of its own. A completely separate union
pointer!
In your code, you initialize the union
pointer belonging to parent
.
But then, you try to access union
pointer belonging to parent2
.
Which you did not initialize, and so you get segmentation fault.
Lets simplify your example event further:
myparentstruct parent;
myparentstruct parent2;
parent.union = &parent.child;
parent2.union->a;
This will still get a segfault. And this code is completely equivalent to what you are attempting.
Nothing you do with pointers will automatically initialize two separate variables at the same time...
Upvotes: 1
Reputation: 106
You've declared parent2
but you haven't initialized it. Additionally, looking through your example code you've provided, you haven't allocated space for anything with malloc()
. Without seeing your actual code, I can't really tell you where you're going wrong. Maybe you are allocating space or did initialize parent2
but it isn't shown.
Upvotes: 0