Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!gem.mps.ohio-state.edu!usc!polyslo!ttwang From: ttwang@polyslo.CalPoly.EDU (Thomas Wang) Newsgroups: comp.lang.c++ Subject: automatic vs static vs heap... Message-ID: <1989Oct26.191119.9278@polyslo.CalPoly.EDU> Date: 26 Oct 89 19:11:19 GMT Reply-To: ttwang@polyslo.CalPoly.EDU (Thomas Wang) Distribution: usa Organization: Cal Poly State University -- San Luis Obispo Lines: 51 Currently there is no portable way to tell if a variable is allocated as automatic, heap, static, or global. I propose that C++ be extended to support the following new operators: void* operator static new (void* where, unsigned int size); void* operator auto new (void* where, unsigned int size); void* operator extern new (void* where, unsigned int size); The traditional void* operator new ... is still reserved for heap variables. These 'new' operators are called before the constructor phase. The 'new' operators can set a flag inside the structure to tell the storage type of the variable. #define HEAP 0 // heap variable #define AUTO 1 // local variable #define STATIC 2 // static variable #define EXTERN 3 // global variable class foo { int storage_type; public: void* operator auto new(foo* where, unsigned int size) { where->storage_type = AUTO; return where; } void* operator static new(foo* where, unsigned int size) { where->storage_type = STATIC; return where; } void* operator extern new(foo* where, unsigned int size) { where->storage_type = EXTERN; return where; } void* operator new(unsigned int size); kill_myself() { if (this->storage_type == HEAP) delete this; } }; void* foo::operator new(unsigned int size) { void* ptr = malloc(size); ptr->storage_type = HEAP; return ptr; } -Thomas Wang (There is a maniac in the bathtub!! -Akane in "Ranma 1/2") ttwang@polyslo.calpoly.edu