473,748 Members | 7,142 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 #1
30 2740
Jess wrote:
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?
You can't, in a standard way. But you can use safer approached as
smart-pointers.

Regards,

Zeppe

May 17 '07 #2
Jess wrote:
>
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?
No. It is up to the programmer to manage pointers. Any access
to pointer value previously passed to delete is undefined. Anything
can happen from segmentation fault to apparently working normally.

Delete takes an rvalue, it can not change it's operanrd.

There's no way to test whether a pointer is bound.

If you wish to keep the pointer around and need to flag that
it has been deleted, the common way is to set the variable to
the null pointer (0).
May 17 '07 #3

"Ron Natalie" <ro*@spamcop.ne twrote in message
news:46******** **************@ news.newshostin g.com...
Jess wrote:
>>
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?
No. It is up to the programmer to manage pointers. Any access
to pointer value previously passed to delete is undefined. Anything
can happen from segmentation fault to apparently working normally.

Delete takes an rvalue, it can not change it's operanrd.

There's no way to test whether a pointer is bound.

If you wish to keep the pointer around and need to flag that
it has been deleted, the common way is to set the variable to
the null pointer (0).
That will not work. You removed the relevant part of the OP's post:
int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}
So, what happens if he adds
p=NULL;
after deleting p?

He still has the same problem, since he is trying to use /q/, not /p/
And it it is not reasonable for you to say that he should set q=NULL
too. These may occur in different modules, and when he deletes p,
there is no way for that module to know whether other pointers have
been set to point to the same place as p.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
May 17 '07 #4

"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.newshostin g.com...
>Jess wrote:
>>>
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?
No. It is up to the programmer to manage pointers. Any access
to pointer value previously passed to delete is undefined. Anything
can happen from segmentation fault to apparently working normally.

Delete takes an rvalue, it can not change it's operanrd.

There's no way to test whether a pointer is bound.

If you wish to keep the pointer around and need to flag that
it has been deleted, the common way is to set the variable to
the null pointer (0).

That will not work. You removed the relevant part of the OP's post:
int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}
So, what happens if he adds
p=NULL;
after deleting p?

He still has the same problem, since he is trying to use /q/, not /p/
And it it is not reasonable for you to say that he should set q=NULL
too. These may occur in different modules, and when he deletes p,
there is no way for that module to know whether other pointers have
been set to point to the same place as p.
--
I don't see anywhere that Victor said setting p to NULL would solve the
problem in that specific example. He just said that the common way to know
that a pointer has been passed to delete was to set it to NULL (after
calling delete). That's true.

In the specific example, he could also set q=NULL. (And then check it
before calling cout.)

If one wants to address a more complex case than that silly example, then
the answer is still the same, really. It's up to the program[mer] to keep
track of whether any pointer has been deleted or not. And one way to do
that is to set it to NULL.

If you're setting multiple pointers to point to a given object, then
*somewhere* you need to keep that information stored. That may be in a
reference-counted pointer, but it may not. After all, you may *want* the
object completely destroyed when passing any one of the pointers to it to
delete, (whereas a reference-counted solution would keep it alive until the
last pointer to it was passed to delete).

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.

In any case, someone needs to *explicitly* keep track of the validity of the
pointer, because it can't do the job for you.

-Howard
May 17 '07 #5
In message <EO************ ****@bgtnsc04-news.ops.worldn et.att.net>,
Howard <al*****@hotmai l.comwrites
>
"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.newshosti ng.com...
>>Jess wrote:
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?

No. It is up to the programmer to manage pointers. Any access
to pointer value previously passed to delete is undefined. Anything
can happen from segmentation fault to apparently working normally.

Delete takes an rvalue, it can not change it's operanrd.

There's no way to test whether a pointer is bound.

If you wish to keep the pointer around and need to flag that
it has been deleted, the common way is to set the variable to
the null pointer (0).

That will not work. You removed the relevant part of the OP's post:
int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}
So, what happens if he adds
p=NULL;
after deleting p?
Nobody seems to have pointed out that he's already in terminal trouble,
since what p pointed to wasn't created by new in the first place :-(
>>
He still has the same problem, since he is trying to use /q/, not /p/
And it it is not reasonable for you to say that he should set q=NULL
too. These may occur in different modules, and when he deletes p,
there is no way for that module to know whether other pointers have
been set to point to the same place as p.

I don't see anywhere that Victor said setting p to NULL would solve the
problem in that specific example. He just said that the common way to know
that a pointer has been passed to delete was to set it to NULL (after
calling delete). That's true.

In the specific example, he could also set q=NULL. (And then check it
before calling cout.)

If one wants to address a more complex case than that silly example, then
the answer is still the same, really. It's up to the program[mer] to keep
track of whether any pointer has been deleted or not. And one way to do
that is to set it to NULL.

If you're setting multiple pointers to point to a given object, then
*somewhere* you need to keep that information stored. That may be in a
reference-counted pointer, but it may not. After all, you may *want* the
object completely destroyed when passing any one of the pointers to it to
delete, (whereas a reference-counted solution would keep it alive until the
last pointer to it was passed to delete).

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.

In any case, someone needs to *explicitly* keep track of the validity of the
pointer, because it can't do the job for you.

-Howard

--
Richard Herring
May 17 '07 #6

"Richard Herring" <ju**@[127.0.0.1]wrote in message
news:hN******** ******@baesyste ms.com...
>>So, what happens if he adds
p=NULL;
after deleting p?

Nobody seems to have pointed out that he's already in terminal trouble,
since what p pointed to wasn't created by new in the first place :-(
D'oh! :-)
May 17 '07 #7
Jess wrote:
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;
At this point anything can happen. "delete p" causes Undefined
Behavior, as p was *NOT* allocated on the free store. Any questions
about what happens next has no valid answer, other than "anything can
happen".
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?

See above.
May 17 '07 #8
Richard Herring wrote:
>

Nobody seems to have pointed out that he's already in terminal trouble,
since what p pointed to wasn't created by new in the first place :-(
I caught it :-), but I hadn't read the rest of this thread yet.
May 17 '07 #9
On May 18, 2:09 am, Richard Herring <ju**@[127.0.0.1]wrote:
In message <EO_2i.1201$p47 ....@bgtnsc04-news.ops.worldn et.att.net>,
Howard <alic...@hotmai l.comwrites


"Fred Kleinschmidt" <fred.l.kleinms chm...@boeing.c omwrote in message
news:JI******** @news.boeing.co m...
"Ron Natalie" <r...@spamcop.n etwrote in message
news:46******* *************** @news.newshosti ng.com...
Jess wrote:
>>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?
>No. It is up to the programmer to manage pointers. Any access
to pointer value previously passed to delete is undefined. Anything
can happen from segmentation fault to apparently working normally.
>Delete takes an rvalue, it can not change it's operanrd.
>There's no way to test whether a pointer is bound.
>If you wish to keep the pointer around and need to flag that
it has been deleted, the common way is to set the variable to
the null pointer (0).
That will not work. You removed the relevant part of the OP's post:
int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}
So, what happens if he adds
p=NULL;
after deleting p?

Nobody seems to have pointed out that he's already in terminal trouble,
since what p pointed to wasn't created by new in the first place :-(


He still has the same problem, since he is trying to use /q/, not /p/
And it it is not reasonable for you to say that he should set q=NULL
too. These may occur in different modules, and when he deletes p,
there is no way for that module to know whether other pointers have
been set to point to the same place as p.
I don't see anywhere that Victor said setting p to NULL would solve the
problem in that specific example. He just said that the common way to know
that a pointer has been passed to delete was to set it to NULL (after
calling delete). That's true.
In the specific example, he could also set q=NULL. (And then check it
before calling cout.)
If one wants to address a more complex case than that silly example, then
the answer is still the same, really. It's up to the program[mer] to keep
track of whether any pointer has been deleted or not. And one way to do
that is to set it to NULL.
If you're setting multiple pointers to point to a given object, then
*somewhere* you need to keep that information stored. That may be in a
reference-counted pointer, but it may not. After all, you may *want* the
object completely destroyed when passing any one of the pointers to it to
delete, (whereas a reference-counted solution would keep it alive until the
last pointer to it was passed to delete).
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.
In any case, someone needs to *explicitly* keep track of the validity of the
pointer, because it can't do the job for you.
-Howard

--
Richard Herring
Right, I forgot I can't delete statically allocated object! :)

May 18 '07 #10

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

Similar topics

1
19180
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
24866
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
29920
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
1664
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
14403
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
11515
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
8830
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
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8242
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...
0
6074
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
4602
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
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
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
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.