473,763 Members | 1,882 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 2007-05-22 15:07:10 -0700, "JohnQ"
<jo************ ***********@yah oo.comsaid:
>
"Jess" <wd***@hotmail. comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
>Hello,

I tried a program as follows:

include<iostre am>

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).
Because it isn't possible (without a garbage collector or some similar
mechanism) to track down all of the pointers that might point to or
into the same object:
int main()
{
int *p1 = new int;
int *p2 = p1;

delete p1;
//p2 still has old value
return 0;
}

--
Clark S. Cox III
cl*******@gmail .com

May 22 '07 #21

"Alf P. Steinbach" <al***@start.no wrote in message
news:5b******** *****@mid.indiv idual.net...
>* 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,
I fail to see where the 'p = 0' added to delete and free would cause
inefficiency.
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; }
I do this in my memory manager (set the pointer to 0). (Overridden global
new and delete operators).
(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.
Is what happens when one tries to dereference a ptr that has been "nulled"
platform-specific?.

John
May 22 '07 #22
On May 23, 12:16 am, "Alf P. Steinbach" <a...@start.now rote:
* 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 ptrand
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).
The answer doesn't have much to do with efficiency, at least not
here. The answer is that just setting the argument to delete to
null doesn't help much (and of course, the argument to delete
isn't even necessarily an lvalue). In order to be helpful, it
would be necessary to set *all* of the pointer to the object to
null. And this isn't reasonably possible with the language
specified as it currently is, and maintaining any degree of C
compatibility. (It might be possible with "fat" pointers, with
each pointer linked back to the originating source of the
pointer in some way.)

--
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 23 '07 #23
On Tue, 22 May 2007 22:07:10 GMT, "JohnQ" wrote:
>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).
The short answer is that you should use delete only in a destructor.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
May 23 '07 #24
* James Kanze:
On May 23, 12:16 am, "Alf P. Steinbach" <a...@start.now rote:
>* 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).

The answer doesn't have much to do with efficiency, at least not
here. The answer is that just setting the argument to delete to
null doesn't help much (and of course, the argument to delete
isn't even necessarily an lvalue).
Nulling a pointer after delete can in some situations help find bugs,
and it can be done automatically when the argument is a non-const
lvalue. That's why it's often done by sloppy coders (Microsoft comes to
mind). What you call "the" answer is another aspect, which is also
relevant, but the inference

In order to be helpful, it
would be necessary to set *all* of the pointer to the object to
null.
is not valid -- one shouldn't refrain from detecting and correcting
bugs just because one can't detect and correct them all (not that I
endorse the technique of nulling pointers, but the argument is invalid).

Cheers,

- Alf

--
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 23 '07 #25
* JohnQ:
> template< typename T >
void destroy( T*& p ) { delete p; p = 0; }

I do this in my memory manager (set the pointer to 0). (Overridden global
new and delete operators).
That is a noop, as void operator delete (void *) passes the pointer of the
to-be-released chunk by value, and not by reference. And even if it did,
it wouldn't work for objects being pointed to by multiple pointers.

--
Martijn van Buul - pi**@dohd.org
May 23 '07 #26
* Roland Pibinger:
The short answer is that you should use delete only in a destructor.
That's short. And wrong.

--
Martijn van Buul - pi**@dohd.org
May 23 '07 #27
JohnQ <jo************ ***********@yah oo.comwrote:
Why doesn't/can't C++ set deleted pointers (and freed memory pointers
therefor) to null?
See The Creator's FAQ:
http://www.research.att.com/~bs/bs_f...ml#delete-zero

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 23 '07 #28

"Marcus Kwok" <ri******@gehen nom.invalidwrot e in message
news:f3******** **@news-int2.gatech.edu ...
JohnQ <jo************ ***********@yah oo.comwrote:
>Why doesn't/can't C++ set deleted pointers (and freed memory pointers
therefor) to null?

See The Creator's FAQ:
http://www.research.att.com/~bs/bs_f...ml#delete-zero
God has an FAQ now? :-)

-Howard
May 23 '07 #29

"Martijn van Buul" <pi**@dohd.orgw rote in message
news:sl******** **********@mud. stack.nl...
>* JohnQ:
>> template< typename T >
void destroy( T*& p ) { delete p; p = 0; }

I do this in my memory manager (set the pointer to 0). (Overridden global
new and delete operators).

That is a noop, as void operator delete (void *) passes the pointer of the
to-be-released chunk by value, and not by reference.
Maybe my compiler is accepting void*&?
And even if it did,
it wouldn't work for objects being pointed to by multiple pointers.
Understood. It's a caveat.

John
May 23 '07 #30

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
9566
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
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
10149
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
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...
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
2
3529
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.