How to distinguish between heap/stack pointers

S

Sayan

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.
 
G

Gianni Mariani

Sayan said:
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 ?
 
D

Daniel T.

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.
 
E

E. Robert Tisdale

Sayan said:
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.
 
J

Jonathan Turkanis

Daniel T. said:
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.

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
 
T

Thomas Matthews

E. Robert Tisdale said:
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
 
R

Ron Natalie

Thomas Matthews said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top