Re: alloca / _alloca / dynamic stack memory
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Thu, 8 Mar 2007 14:14:34 -0500
Michael Crawley <MichaelCrawley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Dynamic stack allocation is nice for performance tuning. If an
object is going to be short-lived and is very small, it would be
ideal to not allocate memory from a lock-based memory allocator like
malloc(size_t)
So at this point, why not declare it as a local variable?
Even if you succeed in your endeavor, imagine the user's surprise when
this innocent-looking code breaks:
MyObj* factory() { return new MyObj; }
MyObj* p = factory();
// Do something to p
delete p;
Of course since MyObj was allocated on the stack inside factory, the
memory is reclaimed as soon as factory() returns (without running
MyObj's destructor, to boot). The rest of the code cheerfully works with
garbage.
Can you show a use case where implementing operator new as _alloca might
be beneficial?
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: alloca / _alloca / dynamic stack memory
- From: Michael Crawley
- Re: alloca / _alloca / dynamic stack memory
- References:
- Re: alloca / _alloca / dynamic stack memory
- From: Igor Tandetnik
- Re: alloca / _alloca / dynamic stack memory
- Prev by Date: Re: alloca / _alloca / dynamic stack memory
- Next by Date: Re: alloca / _alloca / dynamic stack memory
- Previous by thread: Re: alloca / _alloca / dynamic stack memory
- Next by thread: Re: alloca / _alloca / dynamic stack memory
- Index(es):
Relevant Pages
|