Re: alloca / _alloca / dynamic stack memory



On Thu, 8 Mar 2007 12:57:33 -0800, Michael Crawley
<MichaelCrawley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

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

What does String have for data?

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: function "&" for strings type cause memory problem.
    ... > and I allocate the large object in heap memory in previous case. ... Memory allocation is always a risk. ... There is no difference between stack and heap here. ...
    (comp.lang.ada)
  • Re: function "&" for strings type cause memory problem.
    ... > its execution state i. Si/|Si| is an average object size. ... >> and I allocate the large object in heap memory in previous case. ... One can ignore allocation issues as long as ... > the heap can be enlarged on demand while the stack not, ...
    (comp.lang.ada)
  • Re: ten thousand small processes
    ... them roughly in the middle you take space away from heap and stack ... identical across two processes as is the case with large shared memory ... There is no special allocation for virtual address space that is ... the standard libc or at least not the standard malloc implementation. ...
    (freebsd-performance)
  • Re: [RFC] [PATCH] A clean approach to writeout throttling
    ... IOW, we know that it's below its allocation limit, ... very low and the reserve very high, and run a bio through the stack (what ... By also running a real bio through the stack we can discover ... We a-priori decide to limit a particular stack's peak memory usage to ...
    (Linux-Kernel)
  • Re: alloca / _alloca / dynamic stack memory
    ... on the heap, then to allocate Foo the default new operator needs to be called ... Dynamic stack allocation is scoped to a specific stack frame. ... memory allocator to use at runtime to best suite your needs. ...
    (microsoft.public.vc.language)

Loading