[Developers] Dynamic allocation of stack memory

Kristofer Tingdahl kristofer.tingdahl at dgb-group.com
Mon Jul 17 23:40:06 CEST 2006


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


-- 
-- dGB Earth Sciences USA
-- One Sugar Creek Center Boulevard #935, Sugar Land, TX, 77478, USA
-- http://www.dgb-group.com
-- Tel: +1 281 652 5318 , Fax: +1 281 240 3944





More information about the Developers mailing list