473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to distinguish between heap/stack pointers

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.
Jul 22 '05 #1
6 2651
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 ?

Jul 22 '05 #2
ja***@hotpop.com (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.
Jul 22 '05 #3
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.

Jul 22 '05 #4

"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news06.west. earthlink.net...
ja***@hotpop.com (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
Jul 22 '05 #5
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

Jul 22 '05 #6

"Thomas Matthews" <Th****************************@sbcglobal.net> 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.

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
9
by: gold | last post by:
Hello all, I want know abt wht kind of datastructures using both C & C++ internally. Some were said heap, others said tree anyone can explain brief?
12
by: Benny | last post by:
DataSet ds = new DataSet() Is the variable (pointer) 'ds' in the stack and the dataset object being pointed allocated in the heap Thanks Benny
9
by: shine | last post by:
what is the difference between a heap and a stack?
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
4
by: sandeep | last post by:
When we use STL which memory space it will use whither it is stack or heap or data segment How to make STL to create in heap? How to make whole container to create in heap? I think container...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
9
by: Brian Buderman | last post by:
Consider the creation of a class library, where the desire is to allow the user of the library to chain objects together during construction in a single statement. For example, this statement uses...
9
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.