C - Using an union, allocating memory

99封情书 提交于 2019-12-08 02:15:32

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/8215140/c-using-an-union-allocating-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!