473,804 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,p ah);

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

Thanks in advance.
Jul 22 '05 #1
6 2675
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,p ah);

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


Why ?

Jul 22 '05 #2
ja***@hotpop.co m (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,p ah);

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********@eat hlink.net> wrote in message
news:po******** *************** *******@news06. west.earthlink. net...
ja***@hotpop.co m (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.l earn.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.2001 3
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
30103
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 part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
9
2215
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
2536
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
3388
by: shine | last post by:
what is the difference between a heap and a stack?
3
3468
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 memory allocation? like complex<int> * c1,*c2,*c3; i still want to do something like c3 = c1+c2 ; rether than *c3 = *c1+*c2;
4
3696
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 uses stack is it correct ? I am using double linked list so in place of it I want to use STL for #include<stdio.h> #include<iostream>
16
4451
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 in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
9
1778
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 the heap: topLevelObject = new type1(new type2(new type3(), new type4()); While this example only uses 4 types, in actuality there will object trees with a much higher count. Some of the types accept abstract interfaces, and typeinfo must...
9
2545
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 constructor and get me an Array<int32> with a size 0? The memory used is the stack, right? ia = Array<int32>(10);
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9171
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.