I'm trying to add a socket filter to one of my sockets in C++ (Linux). In the socket filter I need to get the offset of struct fork_proc_event, which is nested within another structure. The definition looks like this (cn_proc.h):
struct proc_event {
...
union {
...
struct fork_proc_event {
__kernel_pid_t parent_pid;
...
} fork;
...
} event_data;
...
};
In C I would do this:
int off = offsetof(struct fork_proc_event, parent_pid);
However I'm developing in C++. If I try to do this:
int off = offsetof(proc_event::fork_proc_event, parent_pid);
I get the following error:
error: expected type-specifier error: expected `,' error: expected `)' before ',' token
How should the offsetof() line look like?
It may help to think of how an implementation of an
offsetofmacro might go. Here's one example:In other words, using
0as a pointer to the type you're interested in, and simply taking the address of the struct field...So if you wanted the offset of
parent_pidrelative tofork(which is how I initially parsed your question):On second reading it sounds like you might just want the offset of
parent_pidrelative to the start ofstruct proc_event. Adapting the example above that would be: