[Developers] Dynamic allocation of stack memory
Tracy Stark
tstark3 at attglobal.net
Mon Aug 7 15:18:12 CEST 2006
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
>
More information about the Developers
mailing list