473,763 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to check if a pointer is unbound?

Hello,

I tried a program as follows:

include<iostrea m>

using namespace std;

class A{
public:
int x;
A():x(10){}
};

int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}

Since "q" should be unbound, I thought the output would be "0".
However, the result was segmentation fault. How can I check if a
pointer is unbound?

Thanks,
Jess

May 17 '07
30 2747
On May 18, 1:47 am, "Howard" <alic...@hotmai l.comwrote:
One solution would be to create another object which owns the original
object, and which hands out values to the pointers, and keeps track of which
pointers have been given values. Then, when the owning object is deleted,
it deletes the owned object, and also loops through all the pointers it has
assigned values to, and assigns NULL to them.
I know how to keep a reference count, but can you please show me how
the owner of the object can keep track of which pointers are pointing
to the object and set all of them to NULL when the object is
destroyed?

Thanks,
Jess

May 18 '07 #11
On May 18, 8:27 am, Jess <w...@hotmail.c omwrote:
On May 18, 1:47 am, "Howard" <alic...@hotmai l.comwrote:
One solution would be to create another object which owns the original
object, and which hands out values to the pointers, and keeps track of which
pointers have been given values. Then, when the owning object is deleted,
it deletes the owned object, and also loops through all the pointers ithas
assigned values to, and assigns NULL to them.
I know how to keep a reference count, but can you please show me how
the owner of the object can keep track of which pointers are pointing
to the object and set all of them to NULL when the object is
destroyed?
It's called the Observer pattern. A simple version of it is
used boost::weak_ptr , and an even simpler version in my
ManagedPtr. More generally, however, any object "interested " in
your object registers as an observer, and you inform all
interested objects in the destructor that you're no longer
available. The interested objects then take whatever steps they
deem appropriate: it can be as simple as nulling a pointer, but
will more often (in my experience) involve removing the pointer
from a set or a map, or even obtaining alternative resources or
changing it's own internal state.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 18 '07 #12
Howard wrote:
"Fred Kleinschmidt" <fr************ ******@boeing.c omwrote in message
news:JI******** @news.boeing.co m...
>"Ron Natalie" <ro*@spamcop.ne twrote in message
news:46******* *************** @news.newshosti ng.com...
>>Jess wrote:
I don't see anywhere that Victor said setting p to NULL would solve the
problem in that specific example.
Don't call me Victor (even if we are more or less interchangable) .
May 18 '07 #13
On May 18, 5:57 pm, James Kanze <james.ka...@gm ail.comwrote:
On May 18, 8:27 am, Jess <w...@hotmail.c omwrote:
On May 18, 1:47 am, "Howard" <alic...@hotmai l.comwrote:
One solution would be to create another object which owns the original
object, and which hands out values to the pointers, and keeps track of which
pointers have been given values. Then, when the owning object is deleted,
it deletes the owned object, and also loops through all the pointers it has
assigned values to, and assigns NULL to them.
I know how to keep a reference count, but can you please show me how
the owner of the object can keep track of which pointers are pointing
to the object and set all of them to NULL when the object is
destroyed?

It's called the Observer pattern. A simple version of it is
used boost::weak_ptr , and an even simpler version in my
ManagedPtr. More generally, however, any object "interested " in
your object registers as an observer, and you inform all
interested objects in the destructor that you're no longer
available.
So an "interested " object (call it A) should have a field to indicate
whether the object it's interested in (call it B) is still available
and this field can be set (possibly via some function of A)?
The interested objects then take whatever steps they
deem appropriate:
Should the interested object (A) take this action as soon as it sees
the object (B) is destroyed, or, at some other time (e.g. when A is
about to use B)?
it can be as simple as nulling a pointer, but
will more often (in my experience) involve removing the pointer
from a set or a map,
Does this set or map store all the pointers it has that point to the
external objects (including B here)?

Many thanks,
Jess

May 18 '07 #14
On May 18, 2:22 pm, Jess <w...@hotmail.c omwrote:
On May 18, 5:57 pm, James Kanze <james.ka...@gm ail.comwrote:
On May 18, 8:27 am, Jess <w...@hotmail.c omwrote:
On May 18, 1:47 am, "Howard" <alic...@hotmai l.comwrote:
One solution would be to create another object which owns the original
object, and which hands out values to the pointers, and keeps trackof which
pointers have been given values. Then, when the owning object is deleted,
it deletes the owned object, and also loops through all the pointers it has
assigned values to, and assigns NULL to them.
I know how to keep a reference count, but can you please show me how
the owner of the object can keep track of which pointers are pointing
to the object and set all of them to NULL when the object is
destroyed?
It's called the Observer pattern. A simple version of it is
used boost::weak_ptr , and an even simpler version in my
ManagedPtr. More generally, however, any object "interested " in
your object registers as an observer, and you inform all
interested objects in the destructor that you're no longer
available.
So an "interested " object (call it A) should have a field to indicate
whether the object it's interested in (call it B) is still available
and this field can be set (possibly via some function of A)?
Maybe. One of the most frequent uses of pointers is navigation.
If there is a one to n relationship, the object on the one side
will typically have a container with the pointers. The call
back for "object deleted" just removes the pointer from the
container: no flags, no extra pointers, no nothing.
The interested objects then take whatever steps they
deem appropriate:
Should the interested object (A) take this action as soon as it sees
the object (B) is destroyed, or, at some other time (e.g. when A is
about to use B)?
Normally, as soon as it is notified of the destruction.
Otherwise, it has to set a flag of some sort, and test it later.
it can be as simple as nulling a pointer, but
will more often (in my experience) involve removing the pointer
from a set or a map,
Does this set or map store all the pointers it has that point to the
external objects (including B here)?
Maybe. It depends on the design. An object doesn't contain a
pointer to another object just for the fun of having pointers to
manage. It contains the pointer for a specific purpose,
according to the design of the application. What it should do
if the object in question is destructed depends on the design,
the role of the two objects in that design, and the relationship
between the objects. It's very application specific.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 18 '07 #15
On May 19, 12:04 am, James Kanze <james.ka...@gm ail.comwrote:
On May 18, 2:22 pm, Jess <w...@hotmail.c omwrote:
On May 18, 5:57 pm, James Kanze <james.ka...@gm ail.comwrote:
On May 18, 8:27 am, Jess <w...@hotmail.c omwrote:
On May 18, 1:47 am, "Howard" <alic...@hotmai l.comwrote:
One solution would be to create another object which owns the original
object, and which hands out values to the pointers, and keeps track of which
pointers have been given values. Then, when the owning object is deleted,
it deletes the owned object, and also loops through all the pointers it has
assigned values to, and assigns NULL to them.
I know how to keep a reference count, but can you please show me how
the owner of the object can keep track of which pointers are pointing
to the object and set all of them to NULL when the object is
destroyed?
It's called the Observer pattern. A simple version of it is
used boost::weak_ptr , and an even simpler version in my
ManagedPtr. More generally, however, any object "interested " in
your object registers as an observer, and you inform all
interested objects in the destructor that you're no longer
available.
So an "interested " object (call it A) should have a field to indicate
whether the object it's interested in (call it B) is still available
and this field can be set (possibly via some function of A)?

Maybe. One of the most frequent uses of pointers is navigation.
If there is a one to n relationship, the object on the one side
will typically have a container with the pointers. The call
back for "object deleted" just removes the pointer from the
container: no flags, no extra pointers, no nothing.
The message that the object has been deleted must be passed on to the
owner of pointer container. Do you think a possible way is to
implement member function of the owner of the container, so that this
function can be called when a object is deleted?

Thanks,
Jess

May 19 '07 #16
On May 19, 12:49 pm, Jess <w...@hotmail.c omwrote:
On May 19, 12:04 am, James Kanze <james.ka...@gm ail.comwrote:
On May 18, 2:22 pm, Jess <w...@hotmail.c omwrote:
On May 18, 5:57 pm, James Kanze <james.ka...@gm ail.comwrote:
On May 18, 8:27 am, Jess <w...@hotmail.c omwrote:
On May 18, 1:47 am, "Howard" <alic...@hotmai l.comwrote:
One solution would be to create another object which owns the original
object, and which hands out values to the pointers, and keeps track of which
pointers have been given values. Then, when the owning object is deleted,
it deletes the owned object, and also loops through all the pointers it has
assigned values to, and assigns NULL to them.
I know how to keep a reference count, but can you please show me how
the owner of the object can keep track of which pointers are pointing
to the object and set all of them to NULL when the object is
destroyed?
It's called the Observer pattern. A simple version of it is
used boost::weak_ptr , and an even simpler version in my
ManagedPtr. More generally, however, any object "interested " in
your object registers as an observer, and you inform all
interested objects in the destructor that you're no longer
available.
So an "interested " object (call it A) should have a field to indicate
whether the object it's interested in (call it B) is still available
and this field can be set (possibly via some function of A)?
Maybe. One of the most frequent uses of pointers is navigation.
If there is a one to n relationship, the object on the one side
will typically have a container with the pointers. The call
back for "object deleted" just removes the pointer from the
container: no flags, no extra pointers, no nothing.
The message that the object has been deleted must be passed on to the
owner of pointer container. Do you think a possible way is to
implement member function of the owner of the container, so that this
function can be called when a object is deleted?
Of course. This is a classical example of the observer pattern.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 19 '07 #17

"Ron Natalie" <ro*@spamcop.ne twrote in message
news:46******** *************** @news.newshosti ng.com...
Howard wrote:
>"Fred Kleinschmidt" <fr************ ******@boeing.c omwrote in message
news:JI******* *@news.boeing.c om...
>>"Ron Natalie" <ro*@spamcop.ne twrote in message
news:46****** *************** *@news.newshost ing.com...
Jess wrote:
>I don't see anywhere that Victor said setting p to NULL would solve the
problem in that specific example.

Don't call me Victor (even if we are more or less interchangable) .
Oops! Sorry! :-}

-Howard (I think)
May 21 '07 #18

"Jess" <wd***@hotmail. comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
Hello,

I tried a program as follows:

include<iostrea m>

using namespace std;

class A{
public:
int x;
A():x(10){}
};

int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}

Since "q" should be unbound, I thought the output would be "0".
However, the result was segmentation fault. How can I check if a
pointer is unbound?
Why doesn't/can't C++ set deleted pointers (and freed memory pointers
therefor) to null? It would seem that dereferencing a null pointer and
getting an exception is preferable than dereferencing and REusing a ptr and
not noticing that it had already been freed/deleted. (I know this has been
asked and answered before, but I forgot what the short answer is).

John
May 22 '07 #19
* JohnQ:
>
Why doesn't/can't C++ set deleted pointers (and freed memory pointers
therefor) to null? It would seem that dereferencing a null pointer and
getting an exception is preferable than dereferencing and REusing a ptr and
not noticing that it had already been freed/deleted. (I know this has been
asked and answered before, but I forgot what the short answer is).
C++ doesn't force that inefficiency on you, but it allows you to easily
define that functionality yourself if you want it:

template< typename T >
void destroy( T*& p ) { delete p; p = 0; }

(plus more overloads plus destroyArray).

It isn't necessarily a good idea.

The problem is that by relying on defined behavior that defined behavior
may easily be abused, resulting in nullpointer-checks everywhere.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 22 '07 #20

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

Similar topics

1
19182
by: Stephan | last post by:
Hi, I'm using Visual Studio 2003 (C#) with the integrated Crystal Report software and have the following question: How can I assign a value (string) to an unbound (string) field in Crystal Report at runtime? Example: private void button1_Click(object sender,
15
24871
by: Rey | last post by:
Howdy all. Appreciate your help with several problems I'm having: I'm trying to determine if the Visit subform (subformVisits) has a new record or been changed, i.e. dirty. The form that contains the subform is named Clients. I have this code in the Add Client btn: If Forms!Clients.subformVisits!VisitDirty = True Then MsgBox "Visit subform is dirty!"
7
29932
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
1
2682
by: Jim M | last post by:
To prevent data corruption I have replaced a memo field on my form with an unbound control on my form that I populate with a function that fills it from a memo field from my table. When the form is closed, another function in a query writes it to the memo field. No trouble. When I run spell check on the form it makes the corrections in the memo field, but does not save them when I write from the form: I put this in the AFTERUPDATE...
1
4265
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will require checking a check box to tell which category something will pertain to. Well after each record is entered i want the check to remain with that record and auto clear when going to the next record so i can input something else if i have a...
1
3072
by: planetthoughtful | last post by:
Hi All, I have a mainform with a subform in which I show some task summary data. On the mainform I have a number of unbound controls that reflect values relevant to each task in the subform. The unbound controls are populated in the subform's OnCurrent even from a number of different tables related to the records in tbl_tasks, which is the recordset displayed in the subform.
1
1666
by: Blue Lagoon Products - Customer Services | last post by:
Hi, We have an inhouse database that I designed in access 2000 with the help of all you guys some time ago. It stores orders and prints packing slips etc. I would like to put onto the packing slip wether this is a new customer or an existing customer. We do have a customer table, just one table for customer details, orders, notes etc, this is what works best for us as data is imported from payment files...anyway...
20
14405
by: technocraze | last post by:
Hi guys & commnunity experts, Does anyone knw how to go about checking for existing data in an MS Acess table? I have tried out the following code using vb but doesnt seem to work that well? Can anyone ps take a look at this code / logic and if possible point out the error or make correction? What i need is to check for existing studentId and name before insertion. If the inserted value existed in the table, message "duplicate" else...
1
11516
by: Euge | last post by:
Hi, I really hope someone will be able to help me with this one as I am sure im just missing something simple. I have an unbound form which has 20 yes/no unbound check boxes. The purpose of the form is to allow users to tick the various fields and a subform return the results. The subform, which does requery when a check box is ticked is based off a query. Initially, I wanted all the records to display before any check boxes are ticked so...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10003
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
9943
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
8825
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
7370
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
5271
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...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
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
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.