views:

291

answers:

4

I am developping on Windows with DevStudio, in C/C++ unmanaged.

I want to allocate some memory on the stack instead of the heap because I don't want to have to deal with releasing that memory manually (I know about smart pointers and all those things. I have a very specific case of memory allocation I need to deal with), similar to the use of A2W() and W2A() macros.

_alloca does that, but it is deprecated. It is suggested to use malloca instead. But _malloca documentation says that a call to ___freea is mandatory for each call to _malloca. It then defeats my purpose to use _malloca, I will use malloc or new instead.

Anybody knows if I can get away with not calling _freea without leaking and what the impacts are internally?

Otherwise, I will end-up just using deprecated _alloca function.

+1  A: 

To allocate memory on the stack, simply declare a variable of the appropriate type and size.

Mitch Wheat
why the downvote? The poster asked about the stack...
Mitch Wheat
It wasn't my downvote, but I'm guessing just because he was specifically asking about _alloca/_malloca, which have very different stack usage patterns than standard variable declarations. Personally, I'm going to vote you up, too, though, because in most cases, this is what I do if possible.
Reed Copsey
@Reed Copsey: thanks.
Mitch Wheat
you cannot dynamically alloc blocs of memory on the stack with a simple variable. For instance, unsigned char dataBlock[tempStorageSize] wont compile. It needs to know the size of the allocation bloc at compile time.
Philibert Perusse
@philibertperusse : I' aware of that. The poster originally aksed how to allocate on the stack.
Mitch Wheat
+6  A: 

It is always important to call _freea after every call to _malloca.

_malloca is like _alloca, but adds some extra security checks and enhancements for your protection. As a result, it's possible for _malloca to allocate on the heap instead of the stack. If this happens, and you do not call _freea, you will get a memory leak.

In debug mode, _malloca ALWAYS allocates on the heap, so also should be freed.

Search for _ALLOCA_S_THRESHOLD for details on how the thresholds work, and why _malloca exists instead of _alloca, and it should make sense.


Edit:

There have been comments suggesting that the person just allocate on the heap, and use smart pointers, etc.

There are advantages to stack allocations, which _malloca will provide you, so there are reasons for wanting to do this. _alloca will work the same way, but is much more likely to cause a stack overflow or other problem, and unfortunately does not provide nice exceptions, but rather tends to just tear down your process. _malloca is much safer in this regard, and protects you, but the cost is that you still need to free your memory with _freea since it's possible (but unlikely in release mode) that _malloca will choose to allocate on the heap instead of the stack.

If your only goal is to avoid having to free memory, I would recommend using a smart pointer that will handle the freeing of memory for you as the member goes out of scope. This would assign memory on the heap, but be safe, and prevent you from having to free the memory. This will only work in C++, though - if you're using plain ol' C, this approach will not work.

If you are trying to allocate on the stack for other reasons (typically performance, since stack allocations are very, very fast), I would recommend using _malloca and living with the fact that you'll need to call _freea on your values.

Reed Copsey
Just curious, but why the downvotes on Mitch and my posts? I'd like to know why somebody disagrees with this comment... especially if I'm missing something.
Reed Copsey
A: 

If your concern is having to free temp memory, and you know all about things like smart-pointers then why not use a similar pattern where memory is freed when it goes out of scope?

template <class T>
class TempMem
{
  TempMem(size_t size)
  {
    mAddress = new T[size];
  }

  ~TempMem
  {
    delete [] mAddress;
  }

  T* mAddress;
}

void foo( void )
{
  TempMem<int> buffer(1024);

  // alternatively you could override the T* operator..
  some_memory_stuff(buffer.mAddress);

  // temp-mem auto-freed
}
Andrew Grant
+2  A: 

Another thing to consider is using an RAII class to manage the allocation - of course that's only useful if your macro (or whatever) can be restricted to C++.

If you want to avoid hitting the heap for performance reasons, take a look at the techniques used by Matthew Wilson's auto_buffer<> template class (http://www.stlsoft.org/doc-1.9/classstlsoft_1_1auto__buffer.html). This will allocate on the stack unless your runtime size request exceeds a size specified at compiler time - so you get the speed of no heap allocation for the majority of allocations (if you size the template right), but everything still works correctly if your exceed that size.

Since STLsoft has a whole lot of cruft to deal with portability issues, you may want to look at a simpler version of auto_buffer<> which is described in Wilson's book, "Imperfect C++".

I found it quite handy in an embedded project.

Michael Burr
+1 on the auto_buffer<> suggestion. It is basically doing what _malloca does instead of _alloca on windows. There is a check to make sure you won't blow your stack limit, and it will do heap allocations if necessary instead of stack allocations. This works in C, though, too.
Reed Copsey