[Developers] Dynamic allocation of stack memory

Nanne Hemstra nanne.hemstra at opendtect.org
Tue Aug 8 08:48:54 CEST 2006


Hi all,

I'd like to make a correction on Kristofers e-mail:
The macro for allocating memory for a dynamic array is:

mVariableLengthArr(type,varnm,size)

and can be used as:


void myfunc( int arraysize )
{
    mVariableLengthArr(int,myvariable,arraysize)
    //Do something with myvariable
    //myvariable will be deleted automatically
}


This macro is available in the source code from our CVS repository and 
also in our latest development release OpendTect 2.5.0 which can be 
downloaded from our downloadcenter.


Best regards,
Nanne




Tracy Stark wrote:
> Kristofer,
>
> In which version is this implemented?
>
> Tracy
>
>
> On 7/17/06 4:31 PM, "Kristofer Tingdahl" <kristofer.tingdahl at dgb-group.com>
> wrote:
>
>   
>> Hi all,
>>
>> some compilers complain when you allocate memory dynamically on the
>> stack, i.e.
>>
>> void myfunc(int size)
>> {
>>     int myvariable[size];
>>     //Do something with my variable
>> }
>>
>> The in order to avoid this error, one can allocate memory in the heap:
>>
>> void myfunc(int size)
>> {
>>     ArrPtrMan<int> myvariable = new int[size];
>>     //Do something with my variable
>> }
>>
>> But this can be quite costly and lead to memory fragmentation. To solve
>> this, I have added a macro in commondefs.h that will allocate the
>> memory, on the stack for all platforms supporting that, and in the heap
>> for all other platforms.
>>
>> The macro is used as follows:
>>
>> void myfunc(int size)
>> {
>>     mDynamicSizeArray( int, myvar, size );
>> }
>>
>>
>> Regards,
>>
>> Kristofer
>>
>>     
-- 
/*
    Nanne Hemstra
    OpendTect developer
    dGB Earth Sciences B.V.
    Nijverheidstraat 11-2, 7511 JM Enschede, the Netherlands
    Tel: +31 534315155 , Fax: +31 534315104
    http://www.opendtect.org || http://www.dgb-group.com
*/





More information about the Developers mailing list