Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to allocate memory dynamically on stack?

Is there a way to allocate memory on stack instead of heap? I can't find a good book on this, anyone here got an idea?

like image 866
Mark Avatar asked Jun 13 '11 19:06

Mark


People also ask

Can you dynamically allocate memory on stack?

We can allocate variable length space dynamically on stack memory by using function _alloca. This function allocates memory from the program stack. It simply takes number of bytes to be allocated and return void* to the allocated space just as malloc call.

How can we dynamically allocate memory in C?

In C, dynamic memory is allocated from the heap using some standard library functions. The two key dynamic memory functions are malloc() and free(). The malloc() function takes a single parameter, which is the size of the requested memory area in bytes. It returns a pointer to the allocated memory.

Why can't we just do dynamic allocation within the stack?

Why can't we allocate dynamic memory (i.e. memory of size that is only known at runtime) on the stack? It's more complicated to achieve this. The size of each stack frame is burned-in to your compiled program as a consequence of the sort of instructions the finished executable needs to contain in order to work.

How does stack allocate memory?

Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack. The size of memory to be allocated is known to the compiler and whenever a function is called, its variables get memory allocated on the stack.


2 Answers

Use alloca() (sometimes called _alloca() or _malloca() ), but be very careful about it — it frees its memory when you leave a function, not when you go out of scope, so you'll quickly blow up if you use it inside a loop.

For example, if you have a function like

int foo( int nDataSize, int iterations )  {    for ( int i = 0; i < iterations ; ++i )    {       char *bytes = alloca( nDataSize );       // the memory above IS NOT FREED when we pass the brace below!    }     return 0; }  // alloca() memory only gets freed here 

Then the alloca() will allocate an additional nDataSize bytes every time through the loop. None of the alloca() bytes get freed until you return from the function. So, if you have an nDataSize of 1024 and an iterations of 8, you'll allocate 8 kilobytes before returning. If you have an nDataSize= 65536 and iterations = 32768, you'll allocate a total 65536×32768=2,147,483,648 bytes, almost certainly blowing your stack and causing a crash.

anecdote: You can easily get into trouble if you write past the end of the buffer, especially if you pass the buffer into another function, and that subfunction has the wrong idea about the buffer's length. I once fixed a rather amusing bug where we were using alloca() to create temporary storage for rendering a TrueType font glyph before sending it over to GPU memory. Our font library didn't account for the diacritic in the Swedish Å character when calculating glyph sizes, so it told us to allocate n bytes to store the glyph before rendering, and then actually rendered n+128 bytes. The extra 128 bytes wrote into the call stack, overwriting the return address and inducing a really painful nondeterministic crash!

like image 82
Crashworks Avatar answered Oct 17 '22 02:10

Crashworks


Since this is tagged C++, typically you just declare the objects you need in the correct scope. They are allocated on the stack, and guaranteed to be released on scope exit. This is RAII, and a critical advantage of C++ over C. No mallocs or news, and especially no allocas, required.

like image 42
Steve Townsend Avatar answered Oct 17 '22 02:10

Steve Townsend