Re: How to distinguish between heap/stack pointers

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 02/09/04


Date: Mon, 09 Feb 2004 15:55:07 GMT

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


Relevant Pages

  • Re: What Kind of DataStructures C using? ( Heap or Tree ??)
    ... > Some were said heap, ... instructions and data is put on a stack. ... reserve memory on the heap and ... return a pointer to this memory area. ...
    (comp.lang.c)
  • Re: using LPVOID
    ... on the heap. ... The only limitation I see is that the the stack in which the variable ... thread is using the pointer. ... stack based pointers are safe because the synchronization is ...
    (microsoft.public.vc.language)
  • Re: I feel stupid... "Invalid combination of opcode and operand", was, now is FORTH question
    ... TOS in ebx - top of stack - first stack element ... PSP in ebp - parameter stack pointer - pointer to stack, ... execute next at the end of their definition. ... High level Forth definitions merely organize the ...
    (comp.lang.asm.x86)
  • Re: various objects in my VB6 project - Calling IUnknown
    ... I've forgotten too much C to be very effective - yes the pointer would drop off the stack. ... > references (heap) is also destroyed. ... pObj does not coorespond to a heap allocation. ...
    (microsoft.public.vb.general.discussion)
  • Re: Linked List & Dynamic Memory Allocation
    ... Both of you mentioned stack and heap in this ... when I call malloc it uses heap to allocate memory. ... if you have an integer pointer object that lives beyond this scope and you had: ...
    (microsoft.public.vc.mfc)