[alsa-devel] Scope of snd_pcm_hw_params_alloca() ?
Takashi Iwai
tiwai at suse.de
Fri Mar 14 12:59:01 CET 2008
At Thu, 13 Mar 2008 15:41:02 -0300,
Aldrin Martoq wrote:
>
> Hello,
>
> On Thu, Mar 13, 2008 at 1:44 PM, Takashi Iwai <tiwai at suse.de> wrote:
> > At Thu, 13 Mar 2008 16:21:51 +0000,
> > Alan Horstmann wrote:
> > > I've spent a couple of days pining this down.
> > > snd_pcm_hw_params_alloca() is part of the user API -right? But accomplished
> > > as a #define in pcm.h.
> > Because alloca can be used only with a macro due to its nature.
> > > However if it is called inside a function, and the pointer stored for future
> > > use, after leaving the function the allocation seems to have gone, and
> > > attempting thereafter to use the pointer to access the hwparams space in
> > > another API function results in seg-fault.
>
> You should use snd_pcm_hw_params_malloc() instead. *_alloca() from
> asoundlib are based in alloca(). From my system man:
>
> $ man alloca
> NAME
> alloca - memory allocator
>
> SYNOPSIS
> #include <alloca.h>
>
> void *alloca(size_t size);
>
> DESCRIPTION
> The alloca() function allocates size bytes of space in the stack frame
> of the caller. This temporary space is automatically freed when the
> function that called alloca() returns to its caller.
>
> [..]
>
> Yup, this is actually very useful in many many many places!
Especially in C, that isn't easy to have implicit destructors.
BTW, a pitfall once I fell into is that the alloca frees the data at
the function exit, not at the block exit. So, the following code eats
stack up and shall evetually dies:
{
...
for (;;) {
char *tmp = alloca(4096);
...
}
...
}
Takashi
More information about the Alsa-devel
mailing list