473,394 Members | 1,750 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,394 software developers and data experts.

Throwing auto_ptr

Hi everyone,
I currently was thinking whether the following is a good idea:
I want to have something like an auto_ptr that checks during the
dereferencing whether the pointer is null and throws something like a
NullPointerException. Does someone have some experiences with
something like this? In general is it a good idea at all?

Regards,
Thomas Kowalski

Apr 2 '07 #1
6 1309
* Thomas Kowalski:
Hi everyone,
I currently was thinking whether the following is a good idea:
I want to have something like an auto_ptr that checks during the
dereferencing whether the pointer is null and throws something like a
NullPointerException. Does someone have some experiences with
something like this? In general is it a good idea at all?
Better have it terminate the program. Even better, do the checking only
in the constructor and make it immutable.

--
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?
Apr 2 '07 #2
On Apr 2, 12:09 pm, "Alf P. Steinbach" <a...@start.nowrote:
* Thomas Kowalski:
I currently was thinking whether the following is a good idea:
I want to have something like an auto_ptr that checks during the
dereferencing whether the pointer is null and throws something like a
NullPointerException. Does someone have some experiences with
something like this? In general is it a good idea at all?
Better have it terminate the program. Even better, do the checking only
in the constructor and make it immutable.
Isn't that just what a reference does. (Some compilers omit the
checking, but there's no reason to. From a quality of
implementation point of view, they should check. I guess most
just consider that the code's going to blow up fast enough
anyway.)

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

Apr 3 '07 #3
* James Kanze:
On Apr 2, 12:09 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* Thomas Kowalski:
>>I currently was thinking whether the following is a good idea:
I want to have something like an auto_ptr that checks during the
dereferencing whether the pointer is null and throws something like a
NullPointerException. Does someone have some experiences with
something like this? In general is it a good idea at all?
>Better have it terminate the program. Even better, do the checking only
in the constructor and make it immutable.

Isn't that just what a reference does.
Nope. A reference does not provide any guaranteed null-reference
checking: it's like a cast in that it says "I guarantee that this
assumption (of validity) holds". And it doesn't support checking for
nullness yourself, for if you have a null-reference on hand you're
already in Undefined Behavior land.

A smart pointer can provide both guaranteed null-pointer checking and
the option of checking that yourself, and in addition, a smart-pointer
can add any functionality, such as argument-independent pre- and
post-actions for calls on the pointee object, or e.g. information
/about/ the pointee object.

IMHO references are for when you're guaranteeing validity, based on
existing validity guarantees, whereas class type wrappers such as smart
pointers are for when you need to enforce that guarantee.

(Some compilers omit the
checking, but there's no reason to. From a quality of
implementation point of view, they should check. I guess most
just consider that the code's going to blow up fast enough
anyway.)
I agree that there are situations where a compiler could and ideally
should insert checking of pointer dereferencing, if instructed to do so
by e.g. compiler options, such as when initializing a reference.

Correctness is far more important than performance, but I wouldn't like
a compiler to add such checks by default -- then better use C# or Java
or some other language that doesn't have efficiency as a main feature
and isn't based on "don't pay for what you don't use".

However, a smart pointer is compiler-independent and allows such
checking to be applied at much finer granularity than a complete app.

--
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?
Apr 3 '07 #4
On Apr 3, 12:10 pm, "Alf P. Steinbach" <a...@start.nowrote:
* James Kanze:
On Apr 2, 12:09 pm, "Alf P. Steinbach" <a...@start.nowrote:
* Thomas Kowalski:
>I currently was thinking whether the following is a good idea:
I want to have something like an auto_ptr that checks during the
dereferencing whether the pointer is null and throws something like a
NullPointerException. Does someone have some experiences with
something like this? In general is it a good idea at all?
Better have it terminate the program. Even better, do the checking only
in the constructor and make it immutable.
Isn't that just what a reference does.
Nope. A reference does not provide any guaranteed null-reference
checking:
It doesn't guarantee it, but it allows it (and I've used
compilers which do it). My point is that what you seem to be
asking for is a pointer which 1) can't be changed after
initialization, and 2) doesn't allow initialization with null.
And that's more or less the definition of a reference.
it's like a cast in that it says "I guarantee that this
assumption (of validity) holds".
But what can you (formally) offer more. That trying to
initialize with a null address is guaranteed to call abort()?
And it doesn't support checking for
nullness yourself, for if you have a null-reference on hand you're
already in Undefined Behavior land.
A smart pointer can provide both guaranteed null-pointer checking and
the option of checking that yourself,
Or, not and. If it checks, and brings the program down, you
can't check later:-).

References don't give the guarantee, but a good compiler could
certainly make them work that way.
and in addition, a smart-pointer
can add any functionality, such as argument-independent pre- and
post-actions for calls on the pointee object, or e.g. information
/about/ the pointee object.
IMHO references are for when you're guaranteeing validity, based on
existing validity guarantees, whereas class type wrappers such as smart
pointers are for when you need to enforce that guarantee.
Let me see if I've understood correctly here. If you use a
reference, it is a programming error if you try to initialize a
reference with a null address, but with your smart pointers, it
is a programming error if you try to initialize one of them with
a null address.
(Some compilers omit the
checking, but there's no reason to. From a quality of
implementation point of view, they should check. I guess most
just consider that the code's going to blow up fast enough
anyway.)
I agree that there are situations where a compiler could and ideally
should insert checking of pointer dereferencing, if instructed to do so
by e.g. compiler options, such as when initializing a reference.
I'd argue the reverse. It should insert the checking code
unless instructed not to by e.g. compiler options or a pragma.
Correctness is far more important than performance, but I wouldn't like
a compiler to add such checks by default -- then better use C# or Java
or some other language that doesn't have efficiency as a main feature
and isn't based on "don't pay for what you don't use".
The main reason I use C++ instead of Java is that C++ code is
more robust. Java hides errors that C++ allows to crash the
program.
However, a smart pointer is compiler-independent and allows
such checking to be applied at much finer granularity than a
complete app.
Note that if the compiler does it, a good optimizer should be
able to eliminate a large percentage of the checks. It's a bit
like array bounds checking---in practice, with even a half
decent optimizer, there is no measurable difference in speed
when they are activated. (I'm supposing, here, a language which
has a real array type, which can be passed to functions, etc.,
and that is known and understood by the compiler. Bounds
checking is C/C++ rapidly becomes expensive, because you need to
add extra information to the pointers.)

Anyhow, my point was only that your "class" actually has exactly
the semantics of references. All you get is a required check
(rather than an optional one).

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

Apr 3 '07 #5
* James Kanze:
>
Let me see if I've understood correctly here. If you use a
reference, it is a programming error if you try to initialize a
reference with a null address, but with your smart pointers, it
is a programming error if you try to initialize one of them with
a null address.
Basically you got that part right.

--
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?
Apr 3 '07 #6
On Apr 2, 5:44 am, "Thomas Kowalski" <t...@gmx.dewrote:
I currently was thinking whether the following is a good idea:
I want to have something like an auto_ptr that checks during the
dereferencing whether the pointer is null and throws something like a
NullPointerException. Does someone have some experiences with
something like this? In general is it a good idea at all?
In answer to your first question, yes: Loki::SmartPtr (from _Modern C+
+ Design_) allows you to supply a checking policy that will do such a
thing. See <http://sourceforge.net/projects/loki-lib>.

The answer to your second question is trickier. It's not *necessarily*
a bad idea, but as Alf says elsethread, there may be better options.

Cheers! --M

Apr 3 '07 #7

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

Similar topics

21
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How...
5
by: gg | last post by:
I am getting the following compilation errors with the following program. My compiler is aCC 03.27 on HP-UX11 - #include <iostream> using namespace std; #include <list> #include <memory>...
11
by: mangesh | last post by:
I read , FAQ : 17.4] How should I handle resources if my constructors may throw exceptions? Above faq says that use smart pointer in construcors . Because if exception is thrown from constructor...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
9
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
17
by: Ankur Arora | last post by:
Hi All, I'm building a sample application that uses a custom auto_ptr implementation. The program crashes with the following output:- Output (Debug Assertion failed) ----------
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.