Re: alloca / _alloca / dynamic stack memory



On Thu, 8 Mar 2007 20:06:13 -0800, Michael Crawley
<MichaelCrawley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

"Doug Harrison [MVP]" wrote:

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?

Huh...? Perhaps a char* pointer and a length field. Perhaps String is not a
good example, but I just wanted show how we would like to use the class... A
variable-sized array would be more appropriate...

Assume you have your base class, and it does everything you want. Maybe I
missed it, but I think it would help to show how String exploits what
Object gives it. Also, are there any restrictions on String when used as a
class member or the basis of an array type?

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: HLA Lib
    ... All memory allocation is freed up when the process quits. ... reduce need to resize blocks for 98% string operations. ... HLA strings already consume. ...
    (alt.lang.asm)
  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)
  • Re: trying to make my "fillvalues" function work...
    ... void checkPtr_error(const char *location, const void *pointer) ... fprintf(stderr, "Memory allocation failure (%s). ...
    (comp.lang.c)
  • Re: C++ vs Java "new" (no flame war please!)
    ... of such objects that would fit in available memory. ... memory and much more time than would the JVM for the equivalent Java ... The "allocation" ... If you have a small object with a string, ...
    (comp.lang.java)
  • Re: Policy on rebooting?
    ... >> appropriate sizes of the pools statically was impossible. ... The problem is not the use of dynamic memory, ... so that the allocation can be ... If you can guarantee that only one pointer to a particular block is ...
    (comp.arch.embedded)