How to distinguish between heap/stack pointers

Discussion in 'C++' started by Sayan, Feb 9, 2004.

  1. Sayan

    Sayan Guest

    How do I distinguish between a heap pointer and a stack pointer?
    I want the following to work:

    template<class T>
    bool isHeapPtr(T* p1,T* p2);//the function I want to write
    //...
    int a = 5;
    int *pas = &a;
    int *pah = new int;
    *pah = 10;
    cout << isHeapPtr(pas,pah);

    I was writing a smart pointer class and such a function would be really
    helpful.

    Thanks in advance.
    Sayan, Feb 9, 2004
    #1
    1. Advertising

  2. Sayan wrote:
    > How do I distinguish between a heap pointer and a stack pointer?


    Probably a bad thing to want.

    > I want the following to work:
    >
    > template<class T>
    > bool isHeapPtr(T* p1,T* p2);//the function I want to write
    > //...
    > int a = 5;
    > int *pas = &a;
    > int *pah = new int;
    > *pah = 10;
    > cout << isHeapPtr(pas,pah);
    >
    > I was writing a smart pointer class and such a function would be really
    > helpful.


    Why ?
    Gianni Mariani, Feb 9, 2004
    #2
    1. Advertising

  3. Sayan

    Daniel T. Guest

    (Sayan) wrote:

    > How do I distinguish between a heap pointer and a stack pointer?


    The code that creates the object can provide the information. For
    example, every constructor could have a flag 'is_heap'. As long as these
    flags are set correctly, things will be fine.

    > I want the following to work:
    >
    > template<class T>
    > bool isHeapPtr(T* p1,T* p2);//the function I want to write
    > //...
    > int a = 5;
    > int *pas = &a;
    > int *pah = new int;
    > *pah = 10;
    > cout << isHeapPtr(pas,pah);
    >
    > I was writing a smart pointer class and such a function would be really
    > helpful.


    Other than the above, it can't be done portably. If you know enough
    about the particular machine you are coding for, you might be able to
    tell based on the value of the address variable. If I remember right,
    one of the Effective C++ books covers this issue pretty thoroughly.
    Daniel T., Feb 9, 2004
    #3
  4. Sayan wrote:

    > How do I distinguish between a heap pointer and a stack pointer?
    > I want the following to work:
    >
    > template<class T>
    > bool isHeapPtr(T* p1, T* p2);//the function I want to write
    > //...
    > int a = 5;
    > int *pas = &a;
    > int *pah = new int;
    > *pah = 10;
    > cout << isHeapPtr(pas, pah);


    One pointer, pas, points to an object allocated from automatic storage.
    The other pointer, pah, points to an object allocated from free storage.
    Which one should isHeapPtr test? Try this:

    bool isStackPointer(void* p) {
    return &p < p;
    }

    > I was writing a smart pointer class
    > and such a function would be really helpful.


    It might help to explain this.
    You are probably wrong.
    E. Robert Tisdale, Feb 9, 2004
    #4
  5. "Daniel T." <> wrote in message
    news:p...
    > (Sayan) wrote:
    >
    > > How do I distinguish between a heap pointer and a stack pointer?

    >
    > The code that creates the object can provide the information. For
    > example, every constructor could have a flag 'is_heap'. As long as

    these
    > flags are set correctly, things will be fine.


    <snip>

    > Other than the above, it can't be done portably. If you know enough
    > about the particular machine you are coding for, you might be able

    to
    > tell based on the value of the address variable. If I remember

    right,
    > one of the Effective C++ books covers this issue pretty thoroughly.


    Yes, more Effective C++, Item 27, contains a long discussion of this
    issue, concluding that it can't be done, except for this footnote:

    "I have since become convince that signature-based techniques are all
    but foolproof. For details, consult [broken link]." (p.152, 15th
    printing)

    If anyone knows what these 'signature-based techniques' are, I'd be
    very interested.

    Jonathan
    Jonathan Turkanis, Feb 9, 2004
    #5
  6. E. Robert Tisdale wrote:
    > Sayan wrote:
    >
    >> How do I distinguish between a heap pointer and a stack pointer?
    >> I want the following to work:
    >>
    >> template<class T>
    >> bool isHeapPtr(T* p1, T* p2);//the function I want to write
    >> //...
    >> int a = 5;
    >> int *pas = &a;
    >> int *pah = new int;
    >> *pah = 10;
    >> cout << isHeapPtr(pas, pah);

    >
    >
    > One pointer, pas, points to an object allocated from automatic storage.
    > The other pointer, pah, points to an object allocated from free storage.
    > Which one should isHeapPtr test? Try this:
    >
    > bool isStackPointer(void* p) {
    > return &p < p;
    > }


    How can one use the '<' operator on pointers?
    There is no portable method to compare the ordering sequence
    on pointers.

    There is also no guarantee that a pointer will point to a stack
    object or a heap object. There is also no requirement for an
    implementation to provide a stack or heap.

    Some people will try to cast the pointer to an integer value
    hoping that the integral value is a unique value in the address
    space. If this works, it will be platform specific.

    The only comparison operators that are valid on pointers
    are equality (==) and inequality (!=). The ordering comparison
    operators (<, <=, >, >=) when applied to pointers assume a
    a sequence or ordering; which is only valid on contiguous
    sequences like arrays. If one has automatic memory at a high
    address and dynamic in a low address, comparing pointers from
    each section to each other is meaningless.

    If one needs to know if a variable is of local, automatic or
    dynamic storage, then either the program is designed wrong
    or the program is platform specific.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Feb 9, 2004
    #6
  7. Sayan

    Ron Natalie Guest

    "Thomas Matthews" <> wrote in message news:v3OVb.20013
    > How can one use the '<' operator on pointers?
    > There is no portable method to compare the ordering sequence
    > on pointers.


    ....other than between pointers pointing to elements of the same array
    or one past the end.

    For example, one implementation I used intermingled blocks of the "heap"
    and "stack" with each other. There was no simple "range test" that could
    be used to differentiate between the two.
    Ron Natalie, Feb 9, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Cook
    Replies:
    15
    Views:
    47,987
    Rolf Magnus
    Jan 29, 2004
  2. Zach
    Replies:
    9
    Views:
    686
    Thad Smith
    Feb 20, 2007
  3. Veeru
    Replies:
    12
    Views:
    1,318
    Default User
    Sep 14, 2006
  4. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    731
    GArlington
    Mar 25, 2008
  5. viki
    Replies:
    6
    Views:
    558
    Erik Wikström
    Jun 28, 2008
Loading...

Share This Page