Re: How to distinguish between heap/stack pointers
From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 02/09/04
- Next message: Donald Canton: "remove trailing whitespace from string"
- Previous message: Karl Heinz Buchegger: "Re: Is this bad const design?"
- In reply to: E. Robert Tisdale: "Re: How to distinguish between heap/stack pointers"
- Next in thread: Ron Natalie: "Re: How to distinguish between heap/stack pointers"
- Reply: Ron Natalie: "Re: How to distinguish between heap/stack pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Donald Canton: "remove trailing whitespace from string"
- Previous message: Karl Heinz Buchegger: "Re: Is this bad const design?"
- In reply to: E. Robert Tisdale: "Re: How to distinguish between heap/stack pointers"
- Next in thread: Ron Natalie: "Re: How to distinguish between heap/stack pointers"
- Reply: Ron Natalie: "Re: How to distinguish between heap/stack pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|