Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!fernwood!lia!jgro From: jgro@lia (Jeremy Grodberg) Newsgroups: comp.lang.c++ Subject: Re: Distinguishing stack and heap objects Message-ID: <1991Jan7.231403.318@lia> Date: 7 Jan 91 23:14:03 GMT References: <1990Dec29.013620.23761@sjsumcs.sjsu.edu> Reply-To: jgro@lia.com (Jeremy Grodberg) Lines: 36 In article <1990Dec29.013620.23761@sjsumcs.sjsu.edu> horstman@sjsumcs.SJSU.EDU (Cay Horstmann) writes: >Give class X a member int fromHeap and a static member int heapFlag. Define >all constructors X::X( ... ) to contain the lines > fromHeap = heapFlag; > heapFlag = 0; >and define X::operator new( size_t s ) as > { heapFlag = 1; > return new char[s]; > } > >If x is any pointer of type X*, then x->fromHeap is true iff x is allocated >on the heap. I am using the fact that X::operator new is (acts like?) a >static member function and hence can access static members of X. > >Is this legal? Is it in the spirit of the language? Is it useful? CAN I PATENT >IT? Yes. Maybe. No. No. The real problem here is that whle this will work for X* x = new X(...); It will not catch X* x = new X[10]; since that will use ::new, not X::new. -- Jeremy Grodberg "I don't feel witty today. Don't bug me." jgro@lia.com