Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!uunet!olivea!orc!inews!cadev5!psaripe From: psaripe@cadev5.intel.com (Phani Saripella ~) Newsgroups: comp.lang.c++ Subject: Re: Distinguishing stack and heap objects Message-ID: <1598@inews.intel.com> Date: 4 Jan 91 17:28:27 GMT References: <1990Dec29.013620.23761@sjsumcs.sjsu.edu! Sender: news@inews.intel.com Reply-To: psaripe@cadev5.UUCP (Phani Saripella ~) Organization: Microprocessor Component Group, Intel Corp., Santa Clara, CA Lines: 35 In article <1990Dec29.013620.23761@sjsumcs.sjsu.edu! horstman@sjsumcs.SJSU.EDU (Cay Horstmann) writes: !Recently someone asked whether one can distinguish objects allocated with !new from those allocated on the stack, and it just occurred to me that this !is possible. Ignore this if the technique is an old hat, it is new to me. ! !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 Only one catch here. operator new *CANNOT* be overloaded ( nor can delete for that matter) ! { 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? ! !Happy new year, ! !Cay Phani