I was using a sample C ALSA program as reference and ran along the following piece of code:
...
snd_ctl_event_t *event;
snd_ctl_event_alloca(&event);
...
Based on the ALSA source code, snd_ctl_event_alloca
is a macro that calls __snd_alloca
which is a macro that finally expands to the following equivalent line for snd_ctl_event_alloca(&event);
(with some straightforward simplification):
event = (snd_ctl_event_t *) alloca(snd_ctl_event_sizeof());
memset(event, 0, snd_ctl_event_sizeof());
where snd_ctl_event_sizeof()
is only implemented once in the whole library as:
size_t snd_ctl_event_sizeof()
{
return sizeof(snd_ctl_event_t);
}
So my question is, isn't this whole process equivalent to simply doing:
snd_ctl_event_t event = {0};
For reference, these are the macros:
#define snd_ctl_event_alloca(ptr) __snd_alloca(ptr, snd_ctl_event)
#define __snd_alloca(ptr,type) do { *ptr = (type##_t *) alloca(type##_sizeof()); memset(*ptr, 0, type##_sizeof()); } while (0)
Clarifications:
- The first block of code above is at the start of the body of a function and not in a nested block
EDIT
As it turns out (from what I understand), doing:
snd_ctl_event_t event;
gives a storage size of 'event' isn't known
error because snd_ctl_event_t
is apparently an opaque struct that's defined privately. Therefore the only option is dynamic allocation.