0

I have the following code, which works fine and has been in use for a quite a while now... but I have no idea what it means.

struct event_param
{
    int task:3;
    int param1;
    int param2;
};

#define SV_DRIVER_EVENTS_MASK_SIZE (SV_DRIVER_EVENT_LAST*sizeof(struct event_param))
typedef struct event_param driver_event_mask[SV_DRIVER_EVENTS_MASK_SIZE];
typedef driver_event_mask DriverEventMask;
  • What does driver_event_mask represent?
  • Why is there sizeof(struct event_param) inside the array?
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
shd
  • 1,201
  • 4
  • 17
  • 29

2 Answers2

1

The :3 thing represents a bit field. I'm not sure it matters much, though, since sizeof(struct event_param) will still almost certainly be 12 bytes, I think.

So what happens here is that an array of SV_DRIVER_EVENT_LAST of event_param structures is created. But I would also expect that the sizeof(struct event_param) is not necessary here -- the length of an array is defined in the number of units it contains, not in bytes.

1. driver_event_mask is just what it says. A type to represent an array of SV_DRIVER_EVENTS_MASK_SIZE struct event_param items.

2. sizeof(struct event_param) is not needed here, but doesn't hurt (i.e. won't cause bugs) since it just means you allocate about 12 times as much memory as you actually need.

Qnan
  • 3,714
  • 18
  • 15
  • what does this line meens typedef struct event_param driver_event_mask[SV_DRIVER_EVENTS_MASK_SIZE]; ?? – shd Aug 30 '12 at 08:37
  • Just what it says. Let type `driver_event_mask` represent an array of `SV_DRIVER_EVENTS_MASK_SIZE` `struct event_param` items. – Qnan Aug 30 '12 at 08:39
  • isnt it supposed to be the other way ? i meen typedef struct event_param[32] driver_event_mask ? – shd Aug 30 '12 at 08:45
  • @shd check the function typedefs, those also look strange in terms of ordering. – Qnan Aug 30 '12 at 08:51
-2

how to typedef strcuts array in c?

How to define is already shown in your question, for why refer below:-

If a typedef name denotes a variable length array type, the length of the array is fixed at the time the typedef name is defined, not each time it is used:

what does DriverEventMask represents

DriverEventMask represent struct event_param

why there are sizeof(struct event_param)

So that SV_DRIVER_EVENTS_MASK_SIZE will represent the size of struct event_param. This struct contain bit fields and the way it is arranged is not sure until we know if there are any compiler directives to avoid padding.

perilbrain
  • 7,961
  • 2
  • 27
  • 35
  • Compiler **will** know the size of structure, won't it? So multiplying by `sizeof()` here is unnecessary. – Qnan Aug 30 '12 at 08:53
  • No, you dont know for what purpose SV_DRIVER_EVENTS_MASK_SIZE is being defined.So using sizeof tells compiler to evaluate size. – perilbrain Aug 30 '12 at 08:58
  • Ok, so `SV_DRIVER_EVENT_LAST*sizeof(struct event_param)` is the expected size of the array in *bytes*, is it not? So you claim that if you typedef an array, you have to specify its size in bytes, rather than number of items in the array? – Qnan Aug 30 '12 at 09:01
  • I think this is not the case, see also http://stackoverflow.com/questions/1416898/typedefing-an-array-vs-using-a-struct-in-c – Qnan Aug 30 '12 at 09:04
  • That doesn't mean you have to allocate twelve times the memory. I doubt that the padding will change during the program execution. – Qnan Aug 30 '12 at 09:15
  • The quotation refers to the case where the array length typedef includes an actual variable, which is not the case. sizeof(whatever) is not a variable. – Qnan Aug 30 '12 at 09:17