Delirium_4
Delirium_4

Reputation: 91

C - Using an union, allocating memory

I have a C structure that looks like this

typedef struct event_queue{
Event* event;
int size;
int front;
int count;
int delay;
} event_queue;

It's a basic circular queue. The event value is an array of EventPointers, and it's traversed every X time to dequeue one of the events. It's initialized like

p->event = calloc(p->size, sizeof(Event));

Thing is, I want to do a similar queue, with similar functionality, to queue other type of similar events but with slightly different data. Initially I just wanted to have separate queues and traverse them separately, but the functionality is so repeated, it seems like I am just doing it wrong. Imagine the "sister" queue as being exactly the same, but with a pointer to a different type for "event".

Should I use an union for this instead? such as

typedef struct event_queue{
union{
  Event* event;
  VisualEvent* visual;
} data;
unsigned char* datatype; //array of same size as data for every individual member
int size;
int front;
int count;
int delay;
} event_queue;

But in that case, how do I allocate memory for the array? Should I keep them separate, and this is a bad idea?

Upvotes: 2

Views: 2524

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477100

One solution is to make the basic event type a union, perhaps a tagged one:

enum EEventType { TypeOne, TypeTwo };

typedef struct EventTag_
{
  EEventType tag;
} EventTag;

typedef struct EventOne_
{
  EEventType tag;
  // real data for this event type;
} EventOne;

typedef struct EventTwo_
{
  EEventType tag;
  // real data for the sister event type;
} EventTwo;

typedef union Event_
{
  EventTag kind;
  EventOne event1;
  EventTwo event2;
} Event;

Now make an array of Events. For every Event * p, you can inspect e->kind.tag no matter which union member is active at that moment (thanks to a special rule concerning initial sequences of struct union members).

Upvotes: 3

Related Questions