N
Noob
Hello,
I've tried to translate real code into something
small enough to discuss here. Hopefully, it still
makes some sense...
Consider the following.
Two unrelated structs:
struct desc { int a,b,c,d; };
struct pbuf { int e,f,g; };
A struct which "extends" 'struct pbuf':
struct pbuf2 { struct pbuf x; int y; };
And a struct that "binds" 'struct desc' and 'struct pbuf2':
struct foo { struct pbuf2 xx; struct desc yy; }
Finally consider a callback function
void callback_fun(struct pbuf *p)
At some point in my code, I have a 'struct foo' object,
(say struct foo toto)
I pass q = &toto.pbuf2.x (a struct pbuf pointer) to some
API function, and "sometime later" (threads involved)
the library will call callback_fun(q)
I need to "retrieve" toto.yy from callback_fun's parameter.
Is it safe to do
void callback_fun(struct pbuf *p)
{
struct foo *toto = (struct foo *)p;
/* use toto->desc */
}
considering that 'struct pbuf' is the first field of
'struct pbuf2', which is the first field of 'struct foo'?
Or am I in UB land?
Regards.
I've tried to translate real code into something
small enough to discuss here. Hopefully, it still
makes some sense...
Consider the following.
Two unrelated structs:
struct desc { int a,b,c,d; };
struct pbuf { int e,f,g; };
A struct which "extends" 'struct pbuf':
struct pbuf2 { struct pbuf x; int y; };
And a struct that "binds" 'struct desc' and 'struct pbuf2':
struct foo { struct pbuf2 xx; struct desc yy; }
Finally consider a callback function
void callback_fun(struct pbuf *p)
At some point in my code, I have a 'struct foo' object,
(say struct foo toto)
I pass q = &toto.pbuf2.x (a struct pbuf pointer) to some
API function, and "sometime later" (threads involved)
the library will call callback_fun(q)
I need to "retrieve" toto.yy from callback_fun's parameter.
Is it safe to do
void callback_fun(struct pbuf *p)
{
struct foo *toto = (struct foo *)p;
/* use toto->desc */
}
considering that 'struct pbuf' is the first field of
'struct pbuf2', which is the first field of 'struct foo'?
Or am I in UB land?
Regards.