[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: R: invoking the correct memory management from C files
From: |
Jaroslav Hajek |
Subject: |
Re: R: invoking the correct memory management from C files |
Date: |
Wed, 5 May 2010 17:18:05 +0200 |
On Wed, May 5, 2010 at 4:02 PM, Peter L. Soendergaard
<address@hidden> wrote:
> ons, 05 05 2010 kl. 13:42 +0000, skrev Riccardo Corradini:
>> Could you please show an example?
>>
>>
>>
>
> I my C-library the code typically looks something like this:
>
> void dgtreal_long(....)
> {
> LTFAT_COMPLEX *gf = ltfat_malloc(L*sizeof(LTFAT_COMPLEX));
>
> .. do stuff ...
>
> ltfat_free(gf);
> }
>
> If I build it as a standalone library, ltfat_malloc and Ltfat_free are
> given by:
>
> void* ltfat_malloc (size_t n)
> {
> return fftw_malloc(n);
> }
>
> void ltfat_free(void *ptr)
> {
> fftw_free(ptr);
> }
>
> For Octave I would like to define ltfat_malloc as something like (not
> tested!):
>
> void* ltfat_malloc (size_t n)
> {
> OCTAVE_LOCAL_BUFFER (unsigned char, tmp, n);
> return tmp;
> }
>
> Should I just declare ltfat_free to be empty, or is there a better
> solution?
>
No, this is completely wrong. OCTAVE_LOCAL_BUFFER, as the name
suggests, declares a buffer *local* to the enclosing scope, which is
automatically deallocated when the scope ends. Hence, in your
function, tmp is deallocated immediately after return, so it returns
an invalid pointer. Local buffers have a FIFO ordering and they take
advantage of this to employ an efficient allocation scheme.
--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz