Re: Detecting pointer on the heap
On Apr 2, 5:02 pm, Mark Zaytsev <mark.zayt...@gmail.com> wrote:
On Apr 1, 10:03 am, George Neuner <gneun...@comcast.net> wrote:
On Wed, 31 Mar 2010 13:39:59 CST, pfultz2
<pful...@yahoo.com> wrote:
I want detect if a pointer is pointing to memory on the
heap or the stack, so I came up with this approach:
[skip]
The stack check works on Unix/Linux and Windows because
their stacks are contiguous - but there may be other
platforms for which it won't work (although I've yet to meet
such a platform).
Stack of which thread ?
If object allocated on the other thread's stack, your check
will say: HEAP...
Maybe, maybe not. If the function is called from another
thread, it might also say STACK, when the data is actually on
the heap.
Of course, if you're working in a multithreaded environment,
with all of the threads being started by some common function,
and no objects are ever allocated from main, it's easy to see if
an object is on the current thread's stack, some other thread's
stack, or elsewhere, since you can control the addresses of the
stacks.
--
James Kanze
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]