From: Takashi Iwai Subject: Re: Scope of snd_pcm_hw_params_alloca() ? Date: Fri, 14 Mar 2008 12:59:01 +0100 Message-ID: References: <200803131621.51271.gineera@aspect135.co.uk> <17eedd8a0803131141k61d15c75o8b8ef2e950a774b8@mail.gmail.com> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mx1.suse.de (ns.suse.de [195.135.220.2]) by alsa0.perex.cz (Postfix) with ESMTP id 95E59245FC for ; Fri, 14 Mar 2008 12:59:01 +0100 (CET) In-Reply-To: <17eedd8a0803131141k61d15c75o8b8ef2e950a774b8@mail.gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: alsa-devel-bounces@alsa-project.org Errors-To: alsa-devel-bounces@alsa-project.org To: Aldrin Martoq Cc: alsa-devel@alsa-project.org List-Id: alsa-devel@alsa-project.org At Thu, 13 Mar 2008 15:41:02 -0300, Aldrin Martoq wrote: > > Hello, > > On Thu, Mar 13, 2008 at 1:44 PM, Takashi Iwai 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 > > 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