Pointer Pointing to Stack or Heap??

Today on Codeproject, someone posted a question like "Is there any way to find whether a pointer points to stack or heap?" Is there actually an API for the same??

Well, windows dosen't provide an API or a straight forward way to accomplish this. Any how, we can find a pointer points to stack or heap by simple computations and of course with the help of TIB (Thread Information Block - if you want to learn more about TIB, have a look at the Under The Hood article of Matt Pietrek). In the Thread information block, there are two members, the "StackTop" (located at FS[4]) and "StackBase" (located at FS[8]). The "StackTop" is the memory from which the stack started, and the "StackBase" is the stack location the program commits upto that point. So any object created on stack will have an address between these two pointers. So if we get a pointer, just check whether the pointer falls between the above two memory locations. If it does, then it can be considered a pointer to some stack object. So here is my API to find whether the pointer points to stack or heap.

"Way to find if pointer points to memory on stack or not", rather than

"Pointer pointing to stack or heap".

Because this method will only be able to validate if the memory being pointed to is within the stack limits of the thread that calls this function. If the function determines that the pointer is pointing to a memory location that's off the stack limits, then the function only knows that much - it's off the stack limits.

It won't know if the pointer is actually pointing to something on the heap, or if it's just plain uninitialized. In other words, heap is not into the picture at all.