Since you have to put some upper bound to the size of your stack-allocated
size. If you have to allocate an array which is too big for stack, you
better use 'new'.
The factory model will not work as you suggested. The developer has to
know
the difference between allocating on the stack from allocating on the
heap.
Dynamic stack allocation is scoped to a specific stack frame. I am
opperating under the assumption our developers have taken CIS101
Introduction
to Programming after graduating from a graduate-level masters program or
better ;-)
Lets say a class is to be allocated dynamically all of the time...perhaps
it
has a private/protected destructor, it would be nice to pick and choose
which
memory allocator to use at runtime to best suite your needs. If you need
to
use this class for a good while or need to be able to pass the object
between
threads, then the standard operator new or a new operator that takes a
memory
pool of some sort as an argument would do. If we only need to use it
within
the current function and we are sure there is enough room on the stack,
then
lets not compete for a lock in malloc or a shared heap...lets allocate it
on
the stack if each thread does not have their own heap.
Another way of looking at it is if I sometimes need 1 to 10 bytes and
other
times I need 2000+ bytes, I need to find the best place for memory. I
usually start by finding a memory allocator with the least amount of
contention. If a heap is not associated on a per thread basis, the next
best
thing is the stack. However, stack memory is usually very limited, so it
may
not be an option is some cases. However, when I need 1 to 10 bytes, the
stack should do just fine. However, the dynamic stack memory can only be
used within the current function...once the function returns, the
allocated
memory should be reclaimed.
Defined below is the general direction that I am trying to go. Whether a
class is allocated dynamically on the default heap, a specified heap, or
on
the current thread's stack, I want to present all three mechanisms in a
consistant manner for other developers.
class _StackAlloc{};
const _StackAlloc StackAlloc; // Used only to resolve overloaded
method/operator
void* Object::operator new(size_t size, const _StackAlloc& stackAlloc)
{
// Allocation on stack
// ...
// Return Pointer to allocated memory
__asm push pointer onto stack;
__asm jmp (return address); // Jump out of the current stack frame
};
// Calls destructor, but does not free memory until the
// current function returns
void Object::Dispose(const StackAlloc& stackAlloc)
{
// Invoke Distructor
this->~Object(); // Virtual Base destructor
// Perform other operations specific to this
// memory allocation method
// ...
};
class String : public Object
{
public:
// ...
private:
~String(){...}; // Means this type can only be on allocated on dynamic
memory
};
int main()
{
String str("bad"); // Compiler error, cannot access private distructor
String* str1 = new(Heap) String("better"); // Ok
String* str2 = new String("better"); // Ok
String* str3 = new(StackAlloc) String("better"); // OK, faster than
str1/str2
str1->Dispose(Heap);
delete str2; // or maybe str2->Dispose();
str3->Dispose(StackAlloc);
// ...
return 0;
};
"Igor Tandetnik" wrote:
Michael Crawley <MichaelCrawley@discussions.microsoft.com> 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