I’m reading the source code of Libevent2.0 and I find some code below which I can’t understand. Why we can access the member “ev_ncalls” without accessing “_ev” and “ev_signal”?
And I complied some codes like that which can’t pass through gcc. Here is the code:
struct event {
union {
/* used for io events */
struct {
TAILQ_ENTRY(event) ev_io_next;
struct timeval ev_timeout;
} ev_io;
/* used by signal events */
struct {
TAILQ_ENTRY(event) ev_signal_next;
short ev_ncalls;
/* Allows deletes in callback */
short *ev_pncalls;
} ev_signal;
} _ev;
};
struct event *ev;
int ncall = ev->ev_ncalls;
It’s because
event-internal.hhas stuff like:So when you say
ev->ev_ncallsthe compiler seesev->_ev.ev_signal.ev_ncalls.