Click here to Skip to main content
13,772,340 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 8 Apr 2009
Licenced CPOL

Pointer Pointing to Stack or Heap??

, 8 Apr 2009
Rate this:
Please Sign up or sign in to vote.
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.

// This function will return true if the pointer points to stack. Otherwise false
bool IsMemoryOnStack( LPVOID pVoid )
    LPVOID dwStackTop = 0;
    LPVOID dwStackLowCurrent = 0;
        mov EAX, FS:[4]
        mov dwStackTop, eax
            mov EAX, FS:[8]
            mov dwStackLowCurrent, eax
    if( pVoid <= dwStackTop && pVoid >= dwStackLowCurrent )

        // The memory lie between the stack top and stack committed.
        return true;

    // Pointer doesn't point to the stack
    return false;

Sample code that uses the above API.

void main()
    int OnStack;
    bool bOnStack = IsMemoryOnStack( &OnStack );// Returns true
    int *pOnHeap = new int;
    bOnStack = IsMemoryOnStack( pOnHeap );      // Returns false


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Singapore Singapore
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to know if the memory is on the heap ? Pin
fvalerin4-Mar-13 13:20
memberfvalerin4-Mar-13 13:20 
GeneralNice post, but an observation Pin
Rajesh R Subramanian20-Nov-09 19:58
professionalRajesh R Subramanian20-Nov-09 19:58 
GeneralGood and useful article Pin
Adam Roderick J10-Sep-09 20:00
memberAdam Roderick J10-Sep-09 20:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181119.1 | Last Updated 8 Apr 2009
Article Copyright 2009 by Naveen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid