Re: Offsetof
mihirtr@gmail.com wrote:
typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;
In my program somehow I get pointer to "mem" in struct "Buffer". Now
I
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by
Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.
#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffer, mem)))
I am getting following:
Buffer received_buff;
void * my_ptr;
my_ptr = (void *)received_buff.mem;
The correct line should be:
my_ptr = &received_buff.mem;
If you only have the received_buff.mem pointer value, there is no way
to get the pointer to the Buffer structure. For an example, have a
look at the "container_of" macro in the Linux kernel.
Anyway, this is C code only and not how one would write C++. I'm not
even sure how portable this is across other C compilers (offsetof is
an gcc extension). In the Linux kernel (that's what I do for a living
and there is no C++, unfortunately), they use these kind of constructs
to emulate inheritance and virtual destructors:
/*
* Parent class
*/
struct device {
...
};
/* constructor */
int device_register(struct device *dev)
{
...
}
/* virtual destructor */
void device_release(struct device *dev)
{
/* call the virtual destructor */
dev->release(dev);
}
/*
* Child class
*/
struct amba_device {
...
struct device dev; /* inheritance, kind of */
...
};
/* destructor prototype */
void amba_device_release(struct device *dev);
/* constructor */
int amba_device_register(struct amba_device *dev)
{
/* register the virtual destructor */
dev->dev.release = amba_device_release;
/* call parent constructor */
device_register(&dev->dev);
...
}
/* virtual destructor */
void amba_device_release(struct device *d);
{
struct amba_device *dev = container_of(d, struct amba_device, dev);
/* now do the clean-up on the amba_device */
...
}
So, why bother with the above when you get it in C++ directly using
inheritance? Of course, you would have to re-write parts of the code
but in the end it will save you a lot time by eliminating the pointer
arithmetics (and more bugs). In your case, maybe something like below:
struct RawBuffer
{
Member mem;
};
struct Buffer : public RawBuffer
{
SomeId id;
}
void processData(RawBuffer &buf)
{
/* do something with buf.mem */
...
}
void someFunction()
{
Buffer b;
processData(b);
}
As you can see, you can call processData(Buffer) directly even if the
function only takes a RawBuffer argument since RawBuffer is a
superclass of Buffer.
--
Catalin
--
IMPORTANT NOTICE: The contents of this email and any attachments are
confidential and may also be privileged. If you are not the intended
recipient, please notify the sender immediately and do not disclose the
contents to any other person, use it for any purpose, or store or copy
the information in any medium. Thank you.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]